Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String 'Input string was not in a correct format' exception on JSON string

Tags:

json

c#

exception

I'm trying to escape a JSON payload to a string. Currently I'm receiving an 'Input string was not in a correct format' exception error when trying to complete the conversion.

I thought using double curly braces at the start and end of the escape string would solve it but it hasn't.

Here is the code:

 var newGuidIDEmployeeSyncRequest = Guid.NewGuid().ToString();

 string test = String.Format("{\"confirmMessageID\":{\"idValue\":\"{0}\"},\"createDateTime\":\"{1}\",\"requestReceiptDateTime\":\"{2}\",\"protocolCode\":{\"codeValue\":\"http\"},\"requestStatusCode\":{\"codeValue\":\"succeeded\"},\"requestMethodCode\":{\"codeValue\":\"POST\"},\"requestLink\":null,\"resourceMessages\":[{\"resourceMessageID\":{\"idValue\":\"G3R4RG61Y2T3P1QZ\"},\"resourceStatusCode\":{\"codeValue\":\"succeeded\"},\"processMessages\":[{\"userMessage\":{\"messageTxt\":\"Operation Successful for G3R4RG61Y2T3P1QZ\"}}]}]}", newGuidIDEmployeeSyncRequest, DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss"), DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss"));

Can anyone guide me on where I'm going wrong here?

like image 704
user1352057 Avatar asked Dec 05 '22 08:12

user1352057


1 Answers

With string.Format, the '{' and '}' characters need to be escaped as {{ and }}

string.Format("{ a: {0} }", 2); // throws exception
string.Format("{{ a: {0} }}", 2); // returns the string "{ a: 2 }"
like image 176
ChaseMedallion Avatar answered Apr 12 '23 22:04

ChaseMedallion