0

Possible Duplicate:
how to parse json in javascript

My server is configured to send back JSON data, but it's not really in key/value pairs as I expected.

I'm getting this:

["Value1","Value2","Value3"]

So my question is, is there a standard library people use to parse the above to get only the String values Value1, Value2, and Value3?

Right now, I'm just using a block of code that's doing a replace on the [, ], and " characters split on ",". It's pretty clumsy and was looking for something cleaner.

Community
  • 1
  • 1
Raevik
  • 1,945
  • 9
  • 32
  • 53
  • What language do you use server side? – KooiInc May 07 '12 at 13:59
  • That's not JSON, that's just an array. If it were JSON you wouldn't need to parse it, it would already be an object. – Diodeus - James MacFarlane May 07 '12 at 14:00
  • Java server side. Spring, actually. Controller function is configured to produce JSON with a produces="application/json" signature. There's got to be a more elegant solution than character trimmming... – Raevik May 07 '12 at 14:01
  • @Diodeus: JSON is a "notation", so that's an array expressed in JSON. The resultant data object would be quite simply an object. – BoltClock May 07 '12 at 14:52

3 Answers3

4

You could appears to be just an array and not JSON, but to answer your question.

Using Native JSON

var jsObject = JSON.parse(jsonString);
epascarello
  • 204,599
  • 20
  • 195
  • 236
  • Followed the link and found what I needed (which you just posted). I figured i was making something ridiculously simple too hard. Thanks. – Raevik May 07 '12 at 14:05
0

Browsers have native JSON capabilities and is supported in nearly all modern browsers

As for non-supporting browsers, Crockford has a library that parses JSON

Also, Arrays are valid JSON. Try putting this string in the validator:

["Value1","Value2","Value3"]
Joseph
  • 117,725
  • 30
  • 181
  • 234
0

The format of the json string is also relative to the type of data you're encoding.

An object will look slightly different from an array while encoded to json.

are you using jQuery? If so you can use:

myArray = $.parseJSON(myString);

source: http://api.jquery.com/jQuery.parseJSON/

Rodik
  • 4,054
  • 26
  • 49