This is my JSON but I can't access part of it

This is a sample of the result after running data=JSON.parse(response) on a value returned by my server (PHP script).

{“value_1”:0,“value_2”:[“a”,“b”]}

I can get the ‘value_1’ value by using data.value_1 but I just can’t get the ‘value_2’ value. It looks like an array so surely data.value_2 gets the array and then I go form there, but it doesn’t work.

Am I doing something wrong?

When I try this is in the javascript console in chrome I can get data.value_1 and data.value_2 no problem.


> JSON.parse('{"value_1":0,"value_2":["a","b"]}')
Object
  value_1: 0
  value_2: Array[2]
    0: "a"
    1: "b"
    length: 2

Have you tried data.value_2.0 and data.value_2.1 ?

I hope not, because that won’t work (:

Instead of data.value_2[COLOR="#FF0000"].0[/COLOR] and data.value_2[COLOR="#FF0000"].1[/COLOR] you should use data.value_2[COLOR="#FF0000"][0][/COLOR] and data.value_2[COLOR="#FF0000"][1][/COLOR] since data.value_2 is an array, not an object.

You can access an object like it is an array (in this case, data['value_1'] and data['value_2'] both work), but not the other way around.

Ah, eh… trick question :slight_smile: