无效的JSON格式格式、JSON

由网友(越爱越难过)分享简介:我要建JSON对象并将其与jQuery的ajax传递到服务器。I'm building JSON object and passing it to server with JQuery ajax.data: "{'authorID' : '" + authorID +"', 'title' : '" + encod...

我要建JSON对象并将其与jQuery的ajax传递到服务器。

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 + "'}"

但是,当我blogBody变量中包含的code失败,出现错误消息:

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): {u0027authorIDu0027 : u0027148385u0027, u0027titleu0027 : u0027123213u0027, u0027msgu0027 : u0027%3Cp%3Eqqq%3C%2Fp%3E%3Cp%3E%3Cbr%3E%3C%2Fp%3E%3Cp%3Eu0027u0027u0027u0027%3C%2Fp%3Eu0027, u0027previewu0027 : u0027u0027, u0027layoutIDu0027 : u00271u0027, u0027threadIDu0027 : u00270u0027}","StackTrace":"   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth)rn   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)rn   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)rn   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)rn   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)rn   at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)rn   at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)rn   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?

推荐答案

在 JSON ,密钥必须用双引号括起来(),而不是单引号()。同样,字符串值必须用双,不是单一的,报价,你'重新使用单引号。例如,围绕 AUTHORID 和周围的标题文本。

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 + '"}'
阅读全文

相关推荐

最新文章