0

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","",""]}]}
Billa
  • 5,226
  • 23
  • 61
  • 105
  • 2
    Could you provide sample? – Piotr Stapp Apr 26 '13 at 04:01
  • @Garath added a sample JSON construction in code – Billa Apr 26 '13 at 04:07
  • "How can I parse this successfully"? In javascript? That object will be serialized into json just fine and ca be used in js anyway you want it. What exactly are you doing with the json object that it gives you an error? – von v. Apr 26 '13 at 04:15

1 Answers1

3

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.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thanks and great Oleg! I will see my code again and get back to you, if I unable to resolve. – Billa Apr 26 '13 at 06:29
  • @Billa: JSON data which you posted are totally wrong. You should never ever make manual serialization to JSON. See **UPDATED** part of my answer for details. – Oleg Apr 27 '13 at 08:54