简体   繁体   中英

string.Format Input string was not in correct format for string with curly brackets already as part of the format 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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM