dutchlab
dutchlab

Reputation: 590

JSON parse with jquery?

I could use some help with the syntax when using jquery to parse this json data coming back from the server. I have tried several examples from stackoverflow and other sites and for some reason I keep getting undefined as the out put instead of the id numbers. Each one should be its own line.

{
  "ROWCOUNT":7,
  "COLUMNS":["ID"],
  "DATA":{"id":"211","212","213","221","222","223","232"]}
}

Upvotes: 0

Views: 487

Answers (4)

BartekR
BartekR

Reputation: 3947

You have wrong syntax with square brackets:

  "DATA":{"id":"211","212","213","221","222","223","232"]}

should be:

  "DATA":{"id":["211","212","213","221","222","223","232"]}

Upvotes: 0

JaredPar
JaredPar

Reputation: 754645

The problem appears to be that you are missing an open [ after the id declaration. It should look like the following

{"ROWCOUNT":7, "COLUMNS":["ID"], "DATA":{"id":["211","212","213","221","222","223","232"]}}

Example usage

var json = '{"ROWCOUNT":7, "COLUMNS":["ID"], "DATA":{"id":["211","212","213","221","222","223","232"]}}';
var obj = $.parseJSON(json);
console.log(obj);

Upvotes: 0

Nathan Taylor
Nathan Taylor

Reputation: 24606

I don't know about what code you have so far used to to try to parse it, but I can tell you that the JSON code above is syntactically wrong. Use this instead:

{
  "ROWCOUNT":7,
  "COLUMNS":["ID"],
  "DATA":{"id":"211","212","213","221","222","223","232"}
}

You had an extra square brace.

Upvotes: 0

leopic
leopic

Reputation: 3002

If you pass your JSON through a validator such as JSONLint you'll see that the JSON is not correct, which can be the reason for the errror.

This is what JSONLint shows:

Parse error on line 8:
..."211",        "212",        "213",   
----------------------^
Expecting ':

Which is easy to spot when you add some whitespace:

{
    "ROWCOUNT": 7,
    "COLUMNS": [
        "ID"
    ],
    "DATA": {
        "id": "211",
        "212",
        "213",
        "221",
        "222",
        "223",
        "232"
    ]
}
}

Upvotes: 4

Related Questions