0

I encoded a mysql result from php to json.. I need to decode it in javascript. Say my string returned is

[{"0":"x","1":"z"},{"0":"xs","1":"zz"}]

Please help me on how to get the value of a particular row and column.. for instance how to get the value of "0" of the second row.

EDIT:

Sorry for bothering friends my mistake.. the typeof returned as string JSON.parse(data) did the trick..

0

2 Answers 2

1
var data = [{"0":"x","1":"z"},{"0":"xs","1":"zz"}];
alert (data[1]["0"]);

gives you the xs

The [] represents an array structure, with each {} being an element in the array. Then, within each object there is a set of attributes, which you get it via the attribute's identifier. In this case, it's 0, so it could have just be 0 as well.

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

2 Comments

I still suggest not using numbers in the objects as names
I don't disagree w/ @mplungjan. I answered the question literally, thinking that perhaps "0" was just there as an example, and that what was in "'s might not always be numeric. But if they are always numeric, then yes, don't build the structure this way if you have a choice.
0

Why make objects? It is very poor coding standard to NAME attributes with a number.

Since it obviously are arrays do this instead

var x = [["x","z"],["xs","zz"]]

Then

x[1][0] will give you xs

1 Comment

Note small typo -- there's a bracket lurking inside the braces.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.