0

I have encountered a bizarre case when attempting to parse some JSON data sent from a server.

The data is essentially, a set of rows of data - i.e. a list of lists, and looks something like this:

[[1,2,3],[4,5,6],[7,8,9]]

In FF (using Firebug), the received JSON data is valid, and renders correctly.

When I attempt to parse the JSON data using either of this statements, it fails:

  1. JSON.parse() code breaks on error

  2. jQuery.parseJSON() parses without complaining, yet the result of the parse is a null object

The only way I have managed to successfully parse the JSON response, is to use the dreaded eval() statements, which is a BIG security issue.

Anyone knows what may be going on?

5
  • 2
    Testing it in Firebug gives no problems: >>> JSON.parse("[[1,2,3],[4,5,6],[7,8,9]]"); [[1, 2, 3], [4, 5, 6], [7, 8, 9]]. I suspect that you have over-reduced your reduced test case. Commented Feb 11, 2011 at 7:32
  • What does jslint say? Did you check whitespace? Any invalid (invisible) characters? Encoding OK? Commented Feb 11, 2011 at 7:35
  • $.parseJSON() actually uses eval() after cleaning checking the JSON string with regex. Commented Feb 11, 2011 at 8:48
  • why don't you use eval('(' + dataString + ')') what is the security issue for that. I've used this method every time, and it works fine Commented Feb 11, 2011 at 8:54
  • 1
    I found the solution. It seems that when the server responds with a content type of text/json, the string is automatially parsed by jQuery into a JSO object, so I was ineffect trying to parse twice - hence the error. Commented Feb 12, 2011 at 12:29

1 Answer 1

0

I'm just starting my adventure with JavaScript and JSON, but it looks like it's not a valid JSON object. There is no key:value in this list of lists. I wold suggest changing it into list of obects containing list fields. Sth like:

[ 
{ list: [ 1, 2, 3 ] },
{ list: [ 1, 2, 3 ] },
{ list: [ 1, 2, 3 ] }
]

But I might be very wrong.

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

3 Comments

You are indeed very wrong. No need for keys, it's perfectly valid, see David's comment.
There are no "lists". There are arrays and objects. While you can have an array of objects, nothing prevents you having an array of arrays.
Thx! You posted your comment, while I was replaying - I saw it afterwards. Actually it's quite obvious - I don't know were my mind was.

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.