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]);
5
  • 2
    possible duplicate of Dynamic object property name Commented Jul 5, 2013 at 17:16
  • FYI, that's not a string array, that's an object. Commented Jul 5, 2013 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. Commented Jul 5, 2013 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. Commented Jul 5, 2013 at 17:24
  • @FelixKling Hmmm true, I was not complete enough in that comment. Commented Jul 5, 2013 at 17:27

3 Answers 3

2

Use bracket notation:

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

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

0
0

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

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"]
1
  • "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. Commented Jul 5, 2013 at 17:48

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.