如何使用Javascript从2D json对象获取数据

时间:2018-12-05 15:20:22

标签: javascript json ajax

我有一个如下的JSON respone数组

[
  {
    "productmeta_id": "13",
    "product_id": "2",
    "name": "CompanyName",
    "datatype": "text",
    "lenght": "100",
    "max_size": "100",
    "required": "0",
    "placedolder": "e.g Samsung",
    "product_description": "Ear Phones"
  },
  {
    "productmeta_id": "14",
    "product_id": "2",
    "name": "Model ",
    "datatype": "text",
    "lenght": "100",
    "max_size": "100",
    "required": "0",
    "placedolder": "e.g xyz",
    "product_description": "Ear Phones"
  },
  {
    "productmeta_id": "15",
    "product_id": "2",
    "name": "NETWORK Technology",
    "datatype": "text",
    "lenght": "100",
    "max_size": "100",
    "required": "0",
    "placedolder": "e.g Wireless/ Wired",
    "product_description": "Ear Phones"
  }
]

如何使用javascript获取每个子数组的名称

2 个答案:

答案 0 :(得分:1)

//parse it first
const arr = JSON.parse(jsonString);

//map names to the array
const names = arr.map(obj => obj.name);

答案 1 :(得分:-1)

const names = (JSON.parse(response)).map(obj => obj.name);