Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在JSON对象的字符串中搜索特定值?_Javascript_Jquery - Fatal编程技术网

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

Javascript 如何在JSON对象的字符串中搜索特定值?,javascript,jquery,Javascript,Jquery,我正在尝试从此API中提取数据: 我的前端有一个输入字段,我希望用户能够在其中搜索他们的邮政编码 $('#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 th

我正在尝试从此API中提取数据:

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

$('#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_人员配备_评级”:4,
“联邦供应商编号”:“015009”,
“健康检查等级”:5,
“处理日期”:“2017-12-01T00:00:00”,
“供应商状态”:“AL”,
“人员配备”评级:4,
“质量管理单元评级”:5,
“地点”:{
“纬度”:“34.514971”,
“人类地址”:“{”地址“:”门罗街西北701号“,”城市“:”拉塞尔维尔“,”州“:”阿尔“,”邮编“:”35653”,
“需要重新编码”:错误,
“经度”:“-87.736372”
},
“整体_评级”:5,
“提供者名称”:“伯恩斯疗养院有限公司”
}
我只想请求
Human\u address
包含用户搜索的邮政编码的位置


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

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

var response=getData();
var-zip='35150';
var zipMatches=响应.过滤器(函数(o){
返回JSON.parse(o.location.human_address).zip==zip;
});
控制台日志(zipMatches);
函数getData(){
//AJAX调用的代理。。。
返回[{
“rn_人员配备_评级”:4,
“联邦供应商编号”:“015009”,
“健康检查等级”:5,
“处理日期”:“2017-12-01T00:00:00”,
“供应商状态”:“AL”,
“人员配备”评级:4,
“质量管理单元评级”:5,
“地点”:{
“纬度”:“34.514971”,
“人类地址”:“{”地址“:”门罗街西北701号“,”城市“:”拉塞尔维尔“,”州“:”阿尔“,”邮编“:”35653”,
“需要重新编码”:错误,
“经度”:“-87.736372”
},
“整体_评级”:5,
“提供者名称”:“伯恩斯疗养院有限公司”
}, {
“rn_人员配备_评级”:5,
“联邦供应商编号”:“015010”,
“健康检查和评级”:2,
“处理日期”:“2017-12-01T00:00:00”,
“供应商状态”:“AL”,
“人员配备”评级:5,
“质量管理单元评级”:5,
“地点”:{
“纬度”:“33.164666”,
“人类地址”:“{”地址“:”西山核桃街315号“,”城市“:”锡拉库加“,”州“:”阿尔“,”邮政“:”35150“,
“需要重新编码”:错误,
“经度”:“-86.254598”
},
“总体_评级”:4,
“提供者名称”:“库萨谷护理设施”
}]

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

例如:

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);
var结果=[{
“rn_人员配备_评级”:4,
“联邦供应商编号”:“015009”,
“健康检查等级”:5,
“处理日期”:“2017-12-01T00:00:00”,
“供应商状态”:“AL”,
“人员配备”评级:4,
“质量管理单元评级”:5,
“地点”:{
“纬度”:“34.514971”,
“人类地址”:“{”地址“:”门罗街西北701号“,”城市“:”拉塞尔维尔“,”州“:”阿尔“,”邮编“:”35653”,
“需要重新编码”:错误,
“经度”:“-87.736372”
},
“整体_评级”:5,
“提供者名称”:“伯恩斯疗养院有限公司”
}, {
“rn_人员配备_评级”:5,
“联邦供应商编号”:“015010”,
“健康检查和评级”:2,
“处理日期”:“2017-12-01T00:00:00”,
“供应商状态”:“AL”,
“人员配备”评级:5,
“质量管理单元评级”:5,
“地点”:{
“纬度”:“33.164666”,
“人类地址”:“{”地址“:”西山核桃街315号“,”城市“:”锡拉库加“,”州“:”阿尔“,”邮政“:”35150“,
“需要重新编码”:错误,
“经度”:“-86.254598”
},
“总体_评级”:4,
“提供者名称”:“库萨谷护理设施”
}];
var new_result=[];
对于(变量i=0;i
解析
人类地址
并与prop
zip进行比较
。您是否尝试使用
结果.位置.人类地址
?调用
done()
-函数时,应该已经分析了
结果。如果<代码> HuffyLoad 包含一个JSON字符串(所以<代码> .zip 不可用作为一个属性),那么您应该慎重考虑将它固定在服务器端(如果您控制了该部分)。