views:

38

answers:

3

Hi,

I am using PHP json_encode to generate JSON data of a shopping cart :

{"6cb380f1bfbcd7728be7dfbf2be6bad4":{"rowid":"6cb380f1bfbcd7728be7dfbf2be6bad4",
"id":"sku_131ABC","qty":"4","price":"35.95","name":
"T-Shirt","options":{"Size":"M","Color":"Red"},"subtotal":143.8}}

As you notice the initial ID is unique and not predictable. Is there a way in JavaScript where I can use this data? I am trying to get the values of qty, price(etc)

I was thinking if something on the lines of an index reference exists.

Thedata[0].name

I know this doesn't work, but is something like this possible?

(I am using jquery)

A: 

See json_sans_eval here: http://json.org/

Mchl
You could just link directly to http://code.google.com/p/json-sans-eval/ - also, this is of interest - http://www.json.org/js.html - which is all great, but still doesn't directly answer OP's question.
Peter Ajtai
+3  A: 

Not sure if you can do it without loop, but something like this should work.

for (key in data) {
    alert(data[key].name);
    break;
}
Nikita Rybak
beauty, it worked! - Thanks :)
DMin
+2  A: 

This will work for you:

for (k in Thedata) {
    alert(Thedata[k].name);
}
sworoc
beauty, it worked! - Thanks :)
DMin