JSON Parsing Error
Asked Answered
M

4

8

I got problem. I have this JSON automatically generated by Open Flash Chart php library. The problem is, OFC report JSON Parse Error [Syntax Error] while test result using http://www.jsonlint.com/ report that my JSON is fine. But, w3c parser report error too:(

Any help?

Here's the JSON:

{
    "title": "Followers Trend",
    "elements": [
        {
            "type": "area_hollow",
            "fill-alpha": 0.35,
            "values": [

            ],
            "colour": "#5B56B6",
            "text": "Followers",
            "font-size": 12 
        } 
    ],
    "x_axis": {
        "colour": "#A2ACBA",
        "grid-colour": "#D7E4A3",
        "offset": false,
        "steps": 4,
        "labels": {
            "steps": 2,
            "rotate": "vertical",
            "colour": "#A2ACBA",
            "labels": [

            ] 
        } 
    },
    "x_legend": {
        "text": "Week Trend (2009-08-17 - 2009-08-24)",
        "style": "{font-size: 20px; color: #778877}" 
    },
    "y_axis": {
        "min": 0,
        "max": 150,
        "steps": 30 
    }
}
Meilhac answered 24/8, 2009 at 10:30 Comment(1)
it there, behind I have this JSON Link. Anyway, I write it again here.Meilhac
M
8

A few things I learned while playing with JSON is:

  • If you have validate the JSON on various JSON validation services and the result is GOOD. But, when you failed to eval it, try to wrap your JSON using ( and ) => ({jsondata})

    var json = eval( "(" + jsonString + ")" );
    
  • NEVER build the JSON yourself. It's a gate to failure. Always use official or popular JSON library (depending on your language). For example:

  • To display and format JSON data, you can use JSONViewer.
Meilhac answered 25/10, 2011 at 2:38 Comment(0)
M
3

I think the w3c parser is having issues, I couldn't even get it to parse this:

{
    "title" : "Followers Trend"
}

It gave me this error:

Validation errors:

lexer couldn't parse at "{
    "title" : "Followers Trend"
}"
Malva answered 24/8, 2009 at 12:26 Comment(1)
As I stated above, w3c does failed. What I don't understand is, OFC report failed too but JSONLint doesn't. I don't know what i wrong with the json. It looks good to me :(Meilhac
P
2

http://json.bloople.net helps you visualise the code to find and correct errors.

Panicstricken answered 25/8, 2009 at 4:36 Comment(0)
D
0

try this code, JSON.parse() method is not able to handle string which is in a single quote as a value in the right-hand side. also if you want to handle the UTF-8 character code, then it will do.

parseJSON = function() {
    var data = {};
    var reader = new FileReader();
    reader.onload = function() {
        try {
            data = JSON.parse(reader.result.replace(/'/g, "\""));
            console.log(data)
        } catch (ex) {
            console.log('error' + ex);
        }
    };
    reader.readAsText(fileSelector_test[0].files[0], 'utf-8');
}
Depressive answered 17/9, 2020 at 1:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.