Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON.Net deserialize string which contains special characters

How do I parse JSON string with one of the values containing special characters?

JObject obj = JObject.Parse(str);

str value:

{
  "message": "some !@#$%^&*(){}:"?/?/|"':>;><{"d":"v"}"
}

I have got execption: After parsing a value an unexpected character was encountered: {.

like image 744
ohavryl Avatar asked Dec 20 '13 15:12

ohavryl


1 Answers

That JSON is invalid. If a JSON string contains special characters like double quotes ", backslashes \ or slashes /, they need to be escaped with backslashes \. (See JSON.org.) No JSON parser, including Json.Net, will be able to deal with a JSON string that isn't properly formatted in the first place.

Your JSON would need to look like this to be able to be parsed correctly:

{
  "message": "some !@#$%^&*(){}:\"?/?/|\"':>;><{\"d\":\"v\"}"
}

The solution is to correctly serialize the string at the source.

like image 151
Brian Rogers Avatar answered Nov 15 '22 07:11

Brian Rogers