JavaScriptSerializer Invalid JSON primitive

GoBeavs picture GoBeavs · Nov 20, 2012 · Viewed 11.7k times · Source

When I use the JavaScriptSerializer in C# I'm getting a "Invalid JSON primitive" exception. I assume the issue is with my json input string but I don't see the problem.

JavaScriptSerializer  new JavaScjs =riptSerializer();
js.Deserialize<Object>(json)

"{\"new_name\":\"Arlington\",\"new_locationid\":\"089c6c6a-f520-e111-bdd3-00505695001f\"},{\"new_name\":\"Atlanta\",\"new_locationid\":\"0a9c6c6a-f520-e111-bdd3-00505695001f\"},{\"new_name\":\"Baltimore\",\"new_locationid\":\"0c9c6c6a-f520-e111-bdd3-00505695001f\"}"

Answer

tcbrazil picture tcbrazil · May 12, 2014

GoBeavs:

I validated your json here: http://jsonlint.com/

Your json text is wrong: you must enclose it with brackets ([]) when you have an array of json's. It must looks like this:

"[{\"new_name\":\"Arlington\",\"new_locationid\":\"089c6c6a-f520-e111-bdd3-00505695001f\"},{\"new_name\":\"Atlanta\",\"new_locationid\":\"0a9c6c6a-f520-e111-bdd3-00505695001f\"},{\"new_name\":\"Baltimore\",\"new_locationid\":\"0c9c6c6a-f520-e111-bdd3-00505695001f\"}]"