如何从JSON数组访问单个对象

时间:2016-09-08 12:19:16

标签: angularjs json

好的,我有一个JSON:

{
  "Concepts": [
    {
      "Concept": "1",
      "Description": "siopao"
    },
    {
      "Concept": "4",
      "Description": "gulaman"
    },
    {
      "Concept": "9",
      "Description": "sisig"
    },
    {
      "Concept": "12",
      "Description": "noodle"
    },
    {
      "Concept": "15",
      "Description": "sisigan"
    },
    {
      "Concept": "16",
      "Description": "buko shake"
    },
    {
      "Concept": "17",
      "Description": "mango shake"
    },
    {
      "Concept": "19",
      "Description": "burger"
    },
    {
      "Concept": "20",
      "Description": "sample"
    },
    {
      "Concept": "21",
      "Description": "shit"
    }
  ]
}

我如何只获得“描述”:“siopao”?我正在使用AngularJS。

1 个答案:

答案 0 :(得分:2)

如果您想将jsonstring映射到类似:

["siopao", "gulaman", "sisig", "noodle", "sisigan", "buko shake", "mango shake", "burger", "sample", "shit"]

使用

var object = JSON.parse(jsonString);
var descriptions = object.Concepts.map(function(c) {
     return o.Description;
});

如果您只想要带有“siopao”描述的那个,请在地图前添加一个过滤器:

var object = JSON.parse(jsonString);
var description = object.Concepts.filter(function(a) {
     return a.Description === "siopao";
}).map(function(c) {
     return o.Description;
})[0];