0

I need access to h_00 with other variable, any idea?

var json = '{"h_00":[["bus",28,"F"],["bus",71,"M"],["car",16,"M"]]}';
var arr_data = jQuery.parseJSON(json);

var access = "h_00";

alert(arr_data.access[0]);
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Lenin Che
  • 29
  • 1
  • 4
  • 2
    possible duplicate of [Dynamic object property name](http://stackoverflow.com/questions/4244896/dynamic-object-property-name) – Felix Kling Jul 05 '13 at 17:16
  • FYI, that's not a string array, that's an **object**. – Felix Kling Jul 05 '13 at 17:16
  • `access[0]` will translate to `"h"` in your case. `arr_data.access[0]` will try to access a method or variable `access` under `arr_data` which does not exist. – Sumurai8 Jul 05 '13 at 17:22
  • 1
    @Sumurai8: You are right that `access[0]` would return `'h'` but `arr_data.access[0]` would throw an error because `arr_data.access` is `undefined`. `access` in `arr_data.access` has nothing to do with the variable `access`. – Felix Kling Jul 05 '13 at 17:24
  • @FelixKling Hmmm true, I was not complete enough in that comment. – Sumurai8 Jul 05 '13 at 17:27

3 Answers3

2

Use bracket notation:

arr_data[access][0]; // ["bus", 28, "F"]

Also that is not called a string array. It is an object.

Paul
  • 139,544
  • 27
  • 275
  • 264
0

alert(arr_data[access][0]); should work for you...

mohkhan
  • 11,925
  • 2
  • 24
  • 27
0

If you that standard JSON parser it will accomplish your task:

var json = '{"h_00":[["bus",28,"F"],["bus",71,"M"],["car",16,"M"]]}';
var obj = JSON.parse(json);

You can simply access your property:

obj.h_00

And obj.h_00[0]

Will output:

["bus", 28, "F"]
gustavodidomenico
  • 4,640
  • 1
  • 34
  • 50
  • *"I need access to h_00 with other variable"* probably means that the property name is stored in a variable. Considering that the OP tried `arr_data.access`, I think we can assume that they know about dot notation. – Felix Kling Jul 05 '13 at 17:48