0

说到 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 获取所有产品的类别、可用的国家和价格。

我很感激任何帮助。

谢谢。

4

1 回答 1

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);
}
于 2010-08-01T14:04:41.867 回答