Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string.Format Input string was not in correct format for string with curly brackets already as part of the format C#

Tags:

json

c#

I am trying to format a Json input to a JSON RPC. For example, the JSON am goint to post is as following,

"{"filter":{ "Ids": [123, 124], "Types":["EMPLOYEE"]}}"

which I expect to return users with id 123, 124 and of type EMPLOYEE. But for the Ids parameter, I want to may it dynamic so that I can set the value in my C# calling method like the following

string.Format("{\"filter\":{ \"Ids\": [{0}], \"Types\":[\"EMPLOYEE\"]}}", "123, 124");

when doing so, I get the format exception "Input string was not in correct format"....

I know, I can build up the string using string.concat or string builder. Am just curious, if there is any solution to overcome this string.format exception in the event when a string has curly brackets (am assuming this is the cause of the exception) already.

like image 431
Ant Radha Avatar asked Apr 19 '13 09:04

Ant Radha


1 Answers

You have to escape "{" and "}"-chars by using "{{" resp. "}}".

See "Escaping Braces" in http://msdn.microsoft.com/en-us/library/txafckwd.aspx.

like image 55
ulrichb Avatar answered Sep 26 '22 05:09

ulrichb