在Angular中解析来自API的JSON响应

时间:2017-05-09 17:30:09

标签: javascript angularjs json api

我是Angular的新手,无法尝试解析此API的JSON响应:http://icd10api.com/?s=C50&desc=long&r=json

在此代码中:http://codepen.io/FritzAPI/pen/dNZWKZ

我使用了硬编码修改后的响应:

  $('#circle1').click(function() {
    alert('yeah');
  })
  $('#circle2').click(function() {
    alert('yeah2');
  })

但是只要回复包含在搜索数组中 {“搜索”:[...]} ,此功能就不再有效。

2 个答案:

答案 0 :(得分:1)

您的硬编码对象似乎与API的响应略有不同。 API返回一个带有键Search的对象。 Search的值是veggies数组所模仿的值。如下所示:

apiResponse.Search.map(function (veg) {
  veg._lowername = veg.Name.toLowerCase();
  veg._lowertype = veg.Description.toLowerCase();
  return veg;
});

答案 1 :(得分:0)

<强>样本

var jsonObj = {"Search":[{"Name":"C50","Description":"Malignant neoplasm of breast"},{"Name":"C50.0","Description":"Malignant neoplasm of nipple and areola"},{"Name":"C50.01","Description":"Malignant neoplasm of nipple and areola, female"},{"Name":"C50.011","Description":"Malignant neoplasm of nipple and areola, right female breast"},{"Name":"C50.012","Description":"Malignant neoplasm of nipple and areola, left female breast"},{"Name":"C50.019","Description":"Malignant neoplasm of nipple and areola, unspecified female breast"},{"Name":"C50.02","Description":"Malignant neoplasm of nipple and areola, male"},{"Name":"C50.021","Description":"Malignant neoplasm of nipple and areola, right male breast"},{"Name":"C50.022","Description":"Malignant neoplasm of nipple and areola, left male breast"},{"Name":"C50.029","Description":"Malignant neoplasm of nipple and areola, unspecified male breast"}],"totalResults":"82","Response":"True"};

var res = jsonObj.Search.map(item => { 
  item._lowername = item.Name.toLowerCase();
  item._lowertype = item.Description.toLowerCase();
  return item;
});

console.log(res);