Reputation: 1695
I'm building JSON object and passing it to server with JQuery ajax.
data: "{'authorID' : '" + authorID +
"', 'title' : '" + encodeURIComponent(blogTitle) +
"', 'msg' : '" + encodeURIComponent(blogBody) +
"', 'preview' : '" + encodeURIComponent(mediaContent) +
"', 'layoutID' : '" + previewLayoutId +
"', 'threadID' : '" + threadID + "'}"
But when my blogBody variable contains '
the code fails with the error message:
{"Message":"Invalid object passed in, \u0027:\u0027 or \u0027}\u0027 expected. (107): {\u0027authorID\u0027 : \u0027148385\u0027, \u0027title\u0027 : \u0027123213\u0027, \u0027msg\u0027 : \u0027%3Cp%3Eqqq%3C%2Fp%3E%3Cp%3E%3Cbr%3E%3C%2Fp%3E%3Cp%3E\u0027\u0027\u0027\u0027%3C%2Fp%3E\u0027, \u0027preview\u0027 : \u0027\u0027, \u0027layoutID\u0027 : \u00271\u0027, \u0027threadID\u0027 : \u00270\u0027}","StackTrace":" at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}
Could someone say where is the error and how to fix it?
Upvotes: 2
Views: 28159
Reputation: 1
JSON field names should be in double quotes also string values should be double quotes too. See JSON syntax rules here. Also you can validate JSON output by using an online JSON validation websites like DebugJSON.
Upvotes: -1
Reputation: 29151
I had the same issue when using Microsoft's JavaScriptSerializer()
function for turning objects into JSON.
It would convert apostrophes to \u0027
, but jqGrid would happily continue to display them as \u0027
:
My solution was simply to stop using Microsoft's JavaScriptSerializer()
function..
string JSON = new JavaScriptSerializer().Serialize(myListOfObjects).ToString();
..and switch to JSON.Net..
string JSON = JsonConvert.SerializeObject(myListOfObjects);
JSON.Net also avoids the issues with converting DateTimes to JSON. Microsoft's version uses it's own nasty formatting for dates, eg:
/Date(1355496152000)/
Errr, yeah. Time to get Googling again. Or use JSON.Net !
Upvotes: 0
Reputation: 6149
As T.J. Crowder said, your elements should have double quotes, and to make sure your format is correct, you can always validate your json array here (I always do):
Also you better use this to generate the json arrays
Upvotes: -2
Reputation: 1073978
In JSON, the keys must be quoted with double quotes ("
), not single quotes ('
). Similarly, string values must be in double, not single, quotes. You're using single quotes. For example, around authorId
and around the text of the title.
So at a minimum, you need to swap those quotes around, e.g.:
data: '{"authorID" : "' + authorID +
'", "title" : "' + encodeURIComponent(blogTitle) +
'", "msg" : "' + encodeURIComponent(blogBody) +
'", "preview" : "' + encodeURIComponent(mediaContent) +
'", "layoutID" : "' + previewLayoutId +
'", "threadID" : "' + threadID + '"}'
Upvotes: 9