Possible Duplicate:
I have a nested data structure / JSON, how can I access a specific value?




请在我的ajax端获取以下json字符串:

    {
    "products": [
        {
            "id":            48,
            "quantity":      1,
            "priceByLine":   "950,00 €",
            "name":          "Filtre Mixte",
            "price":         "950,00 €"

        }],

    "total": "950,00 €",
    "productTotal": "950,00 €"
   }


要获得总价,我只需要做:

alert(data.total);


但是,当我想获取ID时,我会:

alert(data.products.id);


它给我不确定!

请高手,我该怎么办?

提前致谢 !

最佳答案

产品是一个数组,其中包含一个类型为object的元素。因此,您应该输入:

alert(data.products[0].id);

如果结构为:您的陈述将是有效的:

{
"products":
    {
        "id":            48,
        "quantity":      1,
        "priceByLine":   "950,00 €",
        "name":          "Filtre Mixte",
        "price":         "950,00 €"

    },

"total": "950,00 €",
"productTotal": "950,00 €"
}

10-08 00:15