开发者

Retrieve JSON data by index

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, b开发者_Go百科ut is something like this possible?


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

for (key in data) {
    alert(data[key].name);
    break;
}


This will work for you:

for (key in Data) {
    alert(Data[key].name);
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜