简体   繁体   中英

Invalid JSON Parser Error

I have an object with the property contains quote('), slash (/) or Environmental.NewLine

I constructed this object as JSON string and try to assign it to a jQGrid.

But it shows Invalid JSON Parser error

How can I parse this successfully.

 myObj=new {Text=@"Samp'le value"}

In ASP.Net MVC, return JSON(myObj) is used for constructing JSON.

Do any one have any idea, where we need to configure to handle this ' / Environmental.NewLine(\\n) while parsing JSON?

We need to use any other library to handle like Newtosoft JSON ?

JSON From Server

     {"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","",""]}]}

Corresponds to specification (see here or here ) only " and \\ characters must be escaped. Some other characters could be escaped. So unescaped ' character is not an error. I suppose that there are another reason for the error "Invalid JSON Parser error".

You should include more full JavaScript code which shows how you use jqGrid, code of ASP.NET MVC controller action or more full JSON response which returns the server. In general you can produce correct JSON response without Json.NET (Newtosoft) or with it or any other library.

You should include autoencode: true option to display any text data correctly in jqGrid. You should use datatype: "json" and jsonReader option (see the documentation ). So it's not enough just to produce well formatter JSON or XML data to display the data correctly in jqGrid. It could be required to include jqGrid options which gives information about exact format of data.

UPDATED : JSON data which you posted are really corrupted. jsonlint.org is a good place where you can validate JSON data. Probably you try to produce JSON data manually because the data are absolutely wrong:

{
    "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

The data contains 5 syntax errors:

  • usage of "cell": "" instead of "cell": [""
  • usage of }, "i": 1, instead of }, {"i": 1,
  • usage of "Sample\\value2" instead of "Sample value2" or "Sample\\\\value2"
  • usage of "http://Googole.com/Dashboard.aspx?ParcelNbr={SITE_APN}&ABC=\\XYZ" instead of "http://Googole.com/Dashboard.aspx?ParcelNbr={SITE_APN}&ABC=XYZ" or "http://Googole.com/Dashboard.aspx?ParcelNbr={SITE_APN}&ABC=\\\\XYZ"
  • usage of "Sample & value3"" instead of "Sample & value3" or "Sample & value3\\""

Moreover you used i property instead of id ( "i": 1 must be fixed to "id": 1 ). It's not an error in JSON, but you should produce the data for jqGrid so you should hold the format which will be expected by jqGrid. See the documentation . The fixed JSON response should be something like

{
    "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",
                "",
                ""
            ]
        }
    ]
}

I recommend you analyse the code of examples of usage ASP.NET MVC with jqGrid (see here for example) and to fix your server code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM