如何显示这个json响应?

时间:2016-07-13 22:06:22

标签: jquery json

我想以最简单的方式显示来自以下JSON响应的产品数组数据:

名称,ID,代码

我遇到的问题是进入产品阵列。我不擅长javascript / jquery所以非常感谢任何帮助。

{
  "success": 1,
  "data": {
    "products": [
      {
        "name": "zip product 1",
        "id": "44",
        "code": "ZIP1"
      },
      {
        "name": "zoo product 1",
        "id": "45",
        "code": "ZOO1"
      },
      {
        "name": "zip product 2",
        "id": "46",
        "code": "Zip2"
      }
    ]
  }
}

2 个答案:

答案 0 :(得分:1)

var resultsObject = <that-data>;
var productsArray = resultsObject.data.products;

应该可以访问产品数组

&#13;
&#13;
var resultsObject = {
  "success": 1,
  "data": {
    "products": [{
      "name": "zip product 1",
      "id": "44",
      "code": "ZIP1"
    }, {
      "name": "zoo product 1",
      "id": "45",
      "code": "ZOO1"
    }, {
      "name": "zip product 2",
      "id": "46",
      "code": "Zip2"
    }]
  }
};
var productsArray = resultsObject.data.products;
console.log(productsArray);
&#13;
&#13;
&#13;

答案 1 :(得分:0)

var products = json.data.products;

for(var i=0; i< products.length; products++)
{
   console.log(products[i].id);
   console.log(products[i].name);
   console.log(products[i].code);
}