从json输出中检索数据时出现问题

时间:2010-08-01 13:30:44

标签: json facebook fbjs

说到json,我是个菜鸟。我越用它,我开始喜欢它。我的输出看起来像这样

[
    {
        "product": {
            "id": "6",
            "category": "Books",
            "created": "2010-04-13 15:15:18",
        },
        "availability": [
            {
                "country": "United Kingdom",
                "price": "$13.99",
            },
            {
                "country": "Germany",
                "price": "$13.99",
            }
        ]            
    }
]

实际上我只列出了一种产品,但输出中列出了很多产品。我想循环浏览这个json输出并获得所有产品的类别,可用的国家和使用fbjs的价格。

我感谢任何帮助。

感谢。

1 个答案:

答案 0 :(得分:1)

如果您的JSON是这样的,

var products = [
    {
        "product": {
            "id": "6",
            "category": "Books",
            "created": "2010-04-13 15:15:18",
        },
        "availability": [
            {
                "country": "United Kingdom",
                "price": "$13.99",
            },
            {
                "country": "Germany",
                "price": "$13.99",
            }
        ]            
    },
    {
        "product": {
            "id": "7",
            "category": "Books",
            "created": "2010-04-13 15:15:18",
        },
        "availability": [
            {
                "country": "United Kingdom",
                "price": "$13.99",
            },
            {
                "country": "Germany",
                "price": "$13.99",
            }
        ]            
    }
]

你可以迭代:

for(var index = 0; index < products.length; index++) {
  alert(products[index].product.category);
}