Question

I'm trying to parse my string to a JSON object, but it doesn't work. I get a syntax error when I debug the code. Here is the string I want to parse:

var listString = "{ title: 'MySchema'," + "root: {id:'"  + headID + "'," 
+   "title:'" + topHead[0].Designation  + "'," + "subtitle:'" + headName + "',";
liststring = liststring + "{ id: '" + head + "'," + "title: '" + childs[cnt].Designation + "'," + "subtitle: '" + title + "'," + "type: '" +  childs[cnt].Typav + "'";

liststring = getChildNodes(tasksEntries, head, liststring); liststring = liststring + "},";}liststring = liststring + "]}}";} listString = childliststring;

$.parseJSON(listString );

I don't get any JSON objects in return. Any thoughts?

No correct solution

OTHER TIPS

jour JSON string should be in '{"key":"value"}' format

JSON.parse("{id:'1'}");

SyntaxError: Unexpected token i

JSON.parse("{'id':'1'}");

SyntaxError: Unexpected token '

JSON.parse('{"id":"1"}');

Object {id: "1"}

JSON.parse('{"id":1}');

Object {id: 1}

Use this to parse the json string.

JSON.parse(json_string);

You have many many Syntax errors, one of it is the comma here 'staff'},]}}" Next, every String have to be in " quotes, so every name property and value. ALso Integer values have no quotes.

The correct json would be:

{
    "title":"myTitle",
    "root":{
        "id":1,
        "title":"CEO",
        "subtitle":"John Doe",
        "children":[
            {
                "id":2,
                "title":"COO",
                "subtitle":"Steve Berman",
                "type":"staff"
            },
            {
                "id":3,
                "title":"CTO",
                "subtitle":"Mark Claskov",
                "type":"staff"
            }
        ]
    }
}

You can test you json here: http://json.parser.online.fr/beta/

First you forgot to put ; at the end of $.parseJSON(myyVar):

var myyVar = {title: 'myTitle',root: {id:'1',title:'CEO',subtitle:'John Doe',children: [{ id: '2',title: 'COO',subtitle: 'Steve Berman',type: 'staff'},{ id: '3',title: 'CTO',subtitle: 'Mark Claskov',type: 'staff'},]}};

Second your main variable myyVar is the one you must use in $.parseJSON() and not myvar:

$.parseJSON(myyVar);

And lastly, you can correct improper json format:

var myyVar = {title: 'myTitle',root: {id:'1',title:'CEO',subtitle:'John Doe',children: [{ id: '2',title: 'COO',subtitle: 'Steve Berman',type: 'staff'},{ id: '3',title: 'CTO',subtitle: 'Mark Claskov',type: 'staff'},]}};
var newVar = JSON.stringify(myyVar );

alert($.parseJSON(newVar));

Check the fiddle. [try to change alert() to console.log() to see what's going on thru browser's console]

Your String should be like this.

{
"title": "myTitle",
"root": {
    "id": "1",
    "title": "CEO",
    "subtitle": "JohnDoe",
    "children": [
        {
            "id": "2",
            "title": "COO",
            "subtitle": "SteveBerman",
            "type": "staff"
        },
        {
            "id": "3",
            "title": "CTO",
            "subtitle": "MarkClaskov",
            "type": "staff"
        }
      ]
   }
}

You can test whether your JSON is valid or not in this link http://jsonlint.com/

Your string is malformed. Read the parseJSON API:

Description: Takes a well-formed JSON string and returns the resulting JavaScript object.

version added: 1.4.1jQuery.parseJSON( json ) json Type: String The JSON string to parse.

Your JSON string should be in '{"key":"value"}' format. Passing in a malformed JSON string results in a JavaScript exception being thrown. For example, the following are all malformed JSON strings:

{test: 1} //(test does not have double quotes around it).
{'test': 1} //('test' is using single quotes instead of double quotes).

The JSON standard does not permit "control characters" such as a tab or newline. An example like $.parseJSON( '{ "testing":"1\t2\n3" }' ) will throw an error in most implementations because the JavaScript parser converts the string's tab and newline escapes into literal tab and newline; doubling the backslashes like "1\\t2\\n3" yields expected results. This problem is often seen when injecting JSON into a JavaScript file from a server-side language such as PHP.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top