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?
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 }"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With