如何在JSON对象中的字符串中搜索特定值?

时间:2018-01-16 15:42:45

标签: javascript jquery

我正在尝试从此API中提取数据:https://data.medicare.gov/resource/ax9d-vq6k.json

我的前端有一个输入字段,我希望用户能够搜索他们的邮政编码。

$('#searchClick').on("click", function() {

// collecting the value from the input field.
var zipCode = $('#zipCode').val().trim();

// This is our API call using the name from the input box to make the call for all surgeons with a last name of something specific. 
var queryURL = "https://data.medicare.gov/resource/ax9d-vq6k.json?human_address=" + zipCode;
console.log("Click Is Working");

// Clearing the main Div after every search
$(".resultsDiv").empty();




// This is the Actual API call.  
$.ajax({
    url: queryURL,
    method: "GET"
}).done(function(response) {
    // console.log(response);

我的API调用中的JSON对象返回:

{
  "rn_staffing_rating": 4,
  "federal_provider_number": "015009",
  "health_inspection_rating": 5,
  "processing_date": "2017-12-01T00:00:00",
  "provider_state": "AL",
  "staffing_rating": 4,
  "qm_rating": 5,
  "location": {
    "latitude": "34.514971",
    "human_address": "{\"address\":\"701 MONROE STREET NW\",\"city\":\"RUSSELLVILLE\",\"state\":\"AL\",\"zip\":\"35653\"}",
    "needs_recoding": false,
    "longitude": "-87.736372"
  },
  "overall_rating": 5,
  "provider_name": "BURNS NURSING HOME, INC."
}

我只是想请求Human_address包含用户搜索的邮政编码的位置。

任何帮助将不胜感激。感谢。

2 个答案:

答案 0 :(得分:1)

您要查找的zip存储在location.human_address属性中的JSON数据中,因此在将值与您要查找的ZIP进行比较之前,您需要对其进行解析。您可以使用filter()执行此操作:

var response = getData();
var zip = '35150';
var zipMatches = response.filter(function(o) {
  return JSON.parse(o.location.human_address).zip == zip;
});
console.log(zipMatches);


function getData() {
  // proxy for your AJAX call...
  return [{
    "rn_staffing_rating": 4,
    "federal_provider_number": "015009",
    "health_inspection_rating": 5,
    "processing_date": "2017-12-01T00:00:00",
    "provider_state": "AL",
    "staffing_rating": 4,
    "qm_rating": 5,
    "location": {
      "latitude": "34.514971",
      "human_address": "{\"address\":\"701 MONROE STREET NW\",\"city\":\"RUSSELLVILLE\",\"state\":\"AL\",\"zip\":\"35653\"}",
      "needs_recoding": false,
      "longitude": "-87.736372"
    },
    "overall_rating": 5,
    "provider_name": "BURNS NURSING HOME, INC."
  }, {
    "rn_staffing_rating": 5,
    "federal_provider_number": "015010",
    "health_inspection_rating": 2,
    "processing_date": "2017-12-01T00:00:00",
    "provider_state": "AL",
    "staffing_rating": 5,
    "qm_rating": 5,
    "location": {
      "latitude": "33.164666",
      "human_address": "{\"address\":\"315 WEST HICKORY STREET\",\"city\":\"SYLACAUGA\",\"state\":\"AL\",\"zip\":\"35150\"}",
      "needs_recoding": false,
      "longitude": "-86.254598"
    },
    "overall_rating": 4,
    "provider_name": "COOSA VALLEY NURSING FACILITY"
  }]
}

答案 1 :(得分:0)

执行此操作的常见方法之一是循环遍历响应数组并找到匹配项:

例如:

var results = [{
    "rn_staffing_rating": 4,
    "federal_provider_number": "015009",
    "health_inspection_rating": 5,
    "processing_date": "2017-12-01T00:00:00",
    "provider_state": "AL",
    "staffing_rating": 4,
    "qm_rating": 5,
    "location": {
      "latitude": "34.514971",
      "human_address": "{\"address\":\"701 MONROE STREET NW\",\"city\":\"RUSSELLVILLE\",\"state\":\"AL\",\"zip\":\"35653\"}",
      "needs_recoding": false,
      "longitude": "-87.736372"
    },
    "overall_rating": 5,
    "provider_name": "BURNS NURSING HOME, INC."
  }, {
    "rn_staffing_rating": 5,
    "federal_provider_number": "015010",
    "health_inspection_rating": 2,
    "processing_date": "2017-12-01T00:00:00",
    "provider_state": "AL",
    "staffing_rating": 5,
    "qm_rating": 5,
    "location": {
      "latitude": "33.164666",
      "human_address": "{\"address\":\"315 WEST HICKORY STREET\",\"city\":\"SYLACAUGA\",\"state\":\"AL\",\"zip\":\"35150\"}",
      "needs_recoding": false,
      "longitude": "-86.254598"
    },
    "overall_rating": 4,
    "provider_name": "COOSA VALLEY NURSING FACILITY"
  }];
var new_result = [];
for (var i=0 ; i < result.length ; i++)
{
    if ( result[i]['location']['human_address'].hasOwnProperty('zip') && result[i]['location']['human_address']['zip'] == "value_to_be_searched") 
    {
        new_result.push(result[i]);
    }
}
console.log(new_result);