1

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?

1
  • 1
    Yes, that string is not a valid JSON object so it makes sense that you can't parse it as one. Commented Feb 18, 2014 at 8:45

6 Answers 6

0

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}

Sign up to request clarification or add additional context in comments.

Comments

0

Use this to parse the json string.

JSON.parse(json_string);

Comments

0

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/

Comments

0

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]

1 Comment

The function takes in a string with the '{"key":"value"}' format. So your answer might still be wrong.
0

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/

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.