我有一个对象,其属性包含quote('),斜杠(/)或Environmental.NewLine
我将此对象构造为JSON字符串并尝试将其分配给jQGrid。
但它显示Invalid JSON Parser error
如何成功解析此问题。
myObj=new {Text=@"Samp'le value"}
在ASP.Net MVC中,return JSON(myObj)
用于构造JSON。
是否有任何人有任何想法,我们需要配置以在解析JSON时处理此'
/
Environmental.NewLine(\n)
?
我们需要使用任何其他库来处理Newtosoft JSON?
来自服务器的JSON
{"total":1 ,"page":1,"records":3,"rows":[{"i":0,"cell":"","1","1","DesSinglApostropAndURLhasEnterKeyChar",
"Samp'le value","http://google.com/Dashboard.aspx?ParcelNbr= {SITE_APN}","False","",""]},"i":1,"cell":"","2","2","DesWithSlashAndURLwithSlash",
"Sample\value2","http://Googole.com/Dashboard.aspx?ParcelNbr={SITE_APN}&ABC=\XYZ","False","",""]},{"i":2,"cell":["","3","3","DesWithAmpersand","Sample & value3"","http://Googole.com","False","",""]}]}
答案 0 :(得分:3)
对应于规范(请参阅here或here),只有"
和\
个字符必须转义。其他一些字符可以转义。因此,未转义的'
字符不是错误。我认为还有另一个原因导致错误“无效的JSON解析器错误”。
您应该包含更多完整的JavaScript代码,其中显示了如何使用jqGrid,ASP.NET MVC控制器操作的代码或更多返回服务器的完整JSON响应。通常,您可以在没有Json.NET(Newtosoft)或使用它或任何其他库的情况下生成正确的JSON响应。
您应该包含autoencode: true
选项以在jqGrid中正确显示任何文本数据。您应该使用datatype: "json"
和jsonReader
选项(请参阅the documentation)。因此仅仅生成格式良好的JSON或XML数据以在jqGrid中正确显示数据是不够的。可能需要包含jqGrid选项,它提供有关数据的确切格式的信息。
更新:您发布的JSON数据确实已损坏。 jsonlint.org是一个可以验证JSON数据的好地方。您可能尝试手动生成JSON数据,因为数据绝对错误:
{
"total": 1,
"page": 1,
"records": 3,
"rows": [
{
"i": 0,
"cell": "", <---- it must be "cell": [""
"1",
"1",
"DesSinglApostropAndURLhasEnterKeyChar",
"Samp'le value",
"http://google.com/Dashboard.aspx?ParcelNbr= {SITE_APN}",
"False",
"",
""
]
},
"i": 1, <--- it must be {"i": 1
"cell": "",
"2",
"2",
"DesWithSlashAndURLwithSlash",
"Sample\value2", <--- it must be "Sample\\value2" or "Sample value2"
"http://Googole.com/Dashboard.aspx?ParcelNbr={SITE_APN}&ABC=\XYZ", <--- \XYZ is WRONG
"False",
"",
""
]
},
{
"i": 2,
"cell": [
"",
"3",
"3",
"DesWithAmpersand",
"Sample & value3"","http: //Googole.com","False","",""]}]} <-- "Sample & value3"" is WRONG
数据包含5个语法错误:
"cell": ""
代替"cell": [""
}, "i": 1,
代替}, {"i": 1,
"Sample\value2"
代替"Sample value2"
或"Sample\\value2"
"http://Googole.com/Dashboard.aspx?ParcelNbr={SITE_APN}&ABC=\XYZ"
代替"http://Googole.com/Dashboard.aspx?ParcelNbr={SITE_APN}&ABC=XYZ"
或"http://Googole.com/Dashboard.aspx?ParcelNbr={SITE_APN}&ABC=\\XYZ"
"Sample & value3""
代替"Sample & value3"
或"Sample & value3\""
此外,您使用i
属性而不是id
("i": 1
必须固定为"id": 1
)。这不是JSON中的错误,但是您应该为jqGrid生成数据,因此您应该保持jqGrid所期望的格式。见the documentation。固定的JSON响应应该类似于
{
"total": 1,
"page": 1,
"records": 3,
"rows": [
{
"id": 0,
"cell": [
"",
"1",
"1",
"DesSinglApostropAndURLhasEnterKeyChar",
"Samp'le value",
"http://google.com/Dashboard.aspx?ParcelNbr= {SITE_APN}",
"False",
"",
""
]
},
{
"id": 1,
"cell": [
"",
"2",
"2",
"DesWithSlashAndURLwithSlash",
"Sample value2",
"http://Googole.com/Dashboard.aspx?ParcelNbr={SITE_APN}&ABC=XYZ",
"False",
"",
""
]
},
{
"id": 2,
"cell": [
"",
"3",
"3",
"DesWithAmpersand",
"Sample & value3",
"http: //Googole.com",
"False",
"",
""
]
}
]
}
我建议您分析使用ASP.NET MVC和jqGrid的示例代码(例如,参见here)并修复服务器代码。