我正在使用结果api,它给了我一个JSON文件。有了这个,我需要查看文件并查找特定属性。所以我将JSON推入一个空数组,循环遍历它,并获取第一个对象。这很好用。我有点难过的是在数组内部,JSON文件中的所有对象都存储在一个大对象中。所以很自然地我想我必须循环遍历那个对象以进入其中,但是当我尝试时,我什么也得不回来。我需要一个额外的循环吗?我将包括以下所有内容。此外,我搜索了高低不同的解决方案,所以如果这是重复的道歉。如果是这种情况,我非常满意一个简单的阅读链接。谢谢你的帮助。
JSON:
{
"0":{
"person_id":"001583133371",
"source":"lexis",
"phone_number":"",
"first_name":"Brian",
"middle_name":"JOSEPH",
"last_name":"Name",
"aliases":[
{
"first_name":"B",
"middle_name":"",
"last_name":"Name"
},
{
"first_name":"Brianj",
"middle_name":"",
"last_name":"Name"
},
{
"first_name":"Brian",
"middle_name":"J",
"last_name":"Name"
},
{
"first_name":"Brian",
"middle_name":"J",
"last_name":"Name"
}
],
"street":"",
"city":"Newbury Park",
"state":"CA",
"zip":"91320",
"priorAddresses":[
{
"street":"",
"city":"Sacramento",
"state":"CA"
},
{
"street":"",
"city":"Las Cruces",
"state":"NM"
},
{
"street":"",
"city":"Big Bear Lake",
"state":"CA"
},
{
"street":"",
"city":"Camarillo",
"state":"CA"
}
],
"age":56,
"dob":"1958\/11\/01",
"yob":"1958",
"relatives":[
{
"first_name":"SHARON",
"middle_name":"J",
"last_name":"Name",
"person_id":"001583349299"
},
{
"first_name":"COREY",
"middle_name":"D",
"last_name":"Name",
"person_id":"036454253754"
},
{
"first_name":"KATHLEEN",
"middle_name":"",
"last_name":"Name",
"person_id":"003096670145"
},
{
"first_name":"KELSEY",
"middle_name":"MAYER",
"last_name":"Name",
"person_id":"080603146279"
}
],
"middle_initial":"J.",
"gender":"m",
"f_phone_number":"N\/A",
"f_relatives":"Sharon Name
Corey Name
Kathleen Name
Kelsey Name",
"a_relatives":"Sharon Name,Corey Name,Kathleen Name,Kelsey Name",
"f_priorAddresses":"Sacramento, CA;Las Cruces, NM;Big Bear Lake, CA;Camarillo, CA",
"street_name":"",
"street_number":"",
"report":"fENBfE5ld2J1cnkgUGFya3xNY21haG9ufEJyaWFufDAwMTU4MzEzMzM3MXw1NnwxOTU4LzExLzAxfHw5MTMyMHxTaGFyb24gTWMgTWFob24sQ29yZXkgTWNtYWhvbixLYXRobGVlbiBNY21haG9uLEtlbHNleSBNY21haG9ufFNhY3JhbWVudG8sIENBO0xhcyBDcnVjZXMsIE5NO0JpZyBCZWFyIExha2UsIENBO0NhbWFyaWxsbywgQ0E="
},
"1":{
"person_id":"001683750301",
"source":"lexis",
"phone_number":"",
"first_name":"Brian",
"middle_name":"DANIEL",
"last_name":"Name",
"aliases":[
{
"first_name":"B",
"middle_name":"",
"last_name":"Name"
},
{
"first_name":"Brian",
"middle_name":"Daniel",
"last_name":"Name"
},
{
"first_name":"Brian",
"middle_name":"Daniel",
"last_name":"Name"
},
{
"first_name":"Brian",
"middle_name":"",
"last_name":"Name"
}
],
"street":"",
"city":"Huntington Beach",
"state":"CA",
"zip":"92648",
"priorAddresses":[
{
"street":"",
"city":"Huntington Beach",
"state":"CA"
},
{
"street":"",
"city":"Santa Ana",
"state":"CA"
},
{
"street":"",
"city":"Costa Mesa",
"state":"CA"
},
{
"street":"",
"city":"Long Beach",
"state":"CA"
}
],
"age":40,
"dob":"1974\/03\/24",
"yob":"1974",
"relatives":[
{
"first_name":"ELAINE",
"middle_name":"D",
"last_name":"Name",
"person_id":"001684063727"
},
{
"first_name":"THOMAS",
"middle_name":"W",
"last_name":"Name",
"person_id":"001684361956"
}
],
"middle_initial":"D.",
"gender":"m",
"f_phone_number":"N\/A",
"f_relatives":"Elaine Name
Thomas Name",
"a_relatives":"Elaine Name,Thomas Name",
"f_priorAddresses":"Huntington Beach, CA;Santa Ana, CA;Costa Mesa, CA;Long Beach, CA",
"street_name":"",
"street_number":"",
"report":"fENBfEh1bnRpbmd0b24gQmVhY2h8TWNtYWhvbnxCcmlhbnwwMDE2ODM3NTAzMDF8NDB8MTk3NC8wMy8yNHx8OTI2NDh8RWxhaW5lIE1jbWFob24sVGhvbWFzIE1jbWFob258SHVudGluZ3RvbiBCZWFjaCwgQ0E7U2FudGEgQW5hLCBDQTtDb3N0YSBNZXNhLCBDQTtMb25nIEJlYWNoLCBDQQ=="
}
}
JS:
var whereAt = [];
$.getJSON('/search/resultsJSON/CA/this/guy', function(json) {
whereAt.push(json);
console.log(whereAt);
for (var i = 0; i < whereAt.length; i++) {
console.log(whereAt[0][i].zip);
var test = whereAt[0][i].zip;
MQA.withModule('geocoder', function() {
map.geocodeAndAddLocations(test);
});
} // end for loop
});
我正在尝试获取whereAt数组中每个对象的zip属性,请不要介意MQA的东西。上面的代码有效,但仅用于获取数组中第一个对象的zip属性。我正在尝试找到一种方法来抓取它们并将它们控制到控制台。非常感谢我能得到的任何帮助,如果我能提供其他任何帮助,我很乐意尽我所能。再次感谢您的时间。
答案 0 :(得分:0)
我认为你的意思是:
var test = whereAt[0][i]["zip"];
JSON仍然是一个数组;它只有自定义HR&#34;键&#34;用于检索数据。
答案 1 :(得分:0)
是的,基本上你需要一个循环来处理数组。
例如,假设您在示例中提供的JSON对象名为jsonFile
,并且您想要遍历jsonFile["0"]["aliases"]
数组中的所有对象,那么您将执行{{1} }循环,条件为for
。
但是,如果您计划更多地使用JSON文档,数组和对象,我强烈建议您检查lodash
之类的库或名为underscore
的替代方案。
他们都有一堆例子,最初花一点时间投入检查示例并找出你需要的那些将为你节省很多意想不到的结果,一般来说,时间:)
它们具有对上述数据类型进行相当复杂的过滤/搜索/迭代操作的功能。
答案 2 :(得分:0)
JSON已经是一个&#39;对象,没有必要将其推入阵列。你拥有的JSON结构更像是字典而不是数组,其中&#34; 0&#34;和&#34; 1&#34;是钥匙。
试试这个:
var whereAt = json;
for (var prop in whereAt) {
if(whereAt.hasOwnProperty(prop)){
console.log(whereAt[prop].zip);
}
}
另外,请务必在&#34; f_relatives&#34;中删除这些换行符。属性。您发布的JSON因其无效。
答案 3 :(得分:0)
如果你的目标是递归查看某个属性的对象键,你应该使用这样的递归函数:
function findZipCodes(obj) {
if (typeof obj != 'object')
return;
if (typeof obj['zip'] != 'undefined')
zipCodes.push(obj['zip']);
for (prop in obj) {
findZipCodes(obj[prop]);
}
}
然后获取JSON的代码如下所示:
$(document).ready(function(){
var zipCodes = [];
$.get('index.json', function(data){
findZipCodes(data);
function findZipCodes(obj) {
if (typeof obj != 'object')
return;
if (typeof obj['zip'] != 'undefined')
zipCodes.push(obj['zip']);
for (prop in obj) {
findZipCodes(obj[prop]);
}
}
console.log(zipCodes);
});
});
答案 4 :(得分:0)
您的错误是由使用基于索引的循环(“for(var i = 0; i&lt; ...”))引起的,这对于具有您期望的属性的对象不起作用。解决方案,但为了减轻你对像这个数组一样迭代这个对象的困惑,我推荐这段代码:
$.getJSON('/search/resultsJSON/CA/this/guy', function(json) {
keys = json.keys()
for (var i = 0; i < keys.length; i++) {
console.log(json[keys[i]].zip);
var test = json[keys[i]].zip;
MQA.withModule('geocoder', function() {
map.geocodeAndAddLocations(test);
});
} // end for loop
});
另请参阅Object.keys()和For..in以供参考。