PHP 按索引访问对象数组的问题 - 不工作但看起来应该
PHP Issue accessing array of objects by index - not working but seems like it should
简而言之,我有一个对象数组,它是从网络服务的 JSON 响应中解码出来的,尽管我尽了最大努力,但我无法通过索引访问该数组中的对象。
我专门解码响应:
$ResponseInfo = json_decode($ResponseBody, false); //give to me as an object
在访问作为对象数组的 属性 之前,我可以毫无问题地访问结果对象的属性。对于上下文,它是 属性 包含一栋或多栋建筑物详细信息的信息。
我希望能够通过索引访问建筑物的详细信息,例如:
$ac = $ResponseInfo->building[0]->airConditioning;
这不起作用。我得到一个未定义的索引 0。奇怪的是,array_keys 给我:
数组([0] => 0)
看来 index 方法应该有用,而且它不是什么奇怪的键,但可惜它没有。
A print_r 给我这个:
Array ( [0] => stdClass Object ( [airConditioning] => Central [foundation] => [architecturalStyle] => Ranch/Rambler [yearBuilt] => 1965 [fireplace] => [noOfUnits] => 1 [effectiveYearBuilt] => [sewer] => Municipal/Public [heating] => Central [class] => [comments] => [quarterBaths] => [totalStories] => 1 [kitchens] => 2 [water] => Municipal/Public [classDescription] => [quality] => C- [totalRooms] => 5 [bedrooms] => 3 [condition] => Average [baths] => [threeQuarterBaths] => [halfBaths] => [occupancyStatus] => [fullBaths] => 1 ) ) Array ( [0] => BuildingInformationObject Object ( [airConditioning] => Central [foundation] => [architecturalStyle] => Ranch/Rambler [yearBuilt] => 1965 [fireplace] => [noOfUnits] => 1 [sewer] => Municipal/Public [heating] => Central [totalStories] => 1 [kitchens] => 2 [water] => Municipal/Public [totalRooms] => 5 [bedrooms] => 3 [condition] => Average [halfBaths] => [occupancyStatus] => [fullBaths] => 1 ) )
我也试过将数组分配给一个新变量,但我仍然得到未定义的索引 0。
$BuildingInfo = $ResponseInfo->building;
奇怪的是,起作用的是:
foreach ($BuildingInfo as $ThisBuilding) {
$ThisBuilding-airconditioning(或其他 属性)以这种方式运行良好。所以我什至尝试创建一个全新的数组并手动分配各个属性,如下所示:
$Buildings = array();
$i = 0;
foreach ($BuildingInfo as $ThisBuilding)
{
$Buildings[$i] = new BuildingInformationObject();
//$Buildings[$i] = $ThisBuilding; //just assigning variable didn't work, so copied properties manually
$Buildings[$i]->airConditioning = $ThisBuilding->airConditioning;
$Buildings[$i]->foundation = $ThisBuilding->foundation;
$Buildings[$i]->architecturalStyle = $ThisBuilding->architecturalStyle;
等..
毕竟,尝试 $Buildings[0]->airConditioning 仍然给我可怕的索引 0 undefined,即使在新阵列上也是如此。
我什至尝试过
形式的东西
echo $data->{'0'}->UserName;
如:
$BuildingInfo->{'0'}->airConditioning;
我在 SO 上找到的。此方法显然对旧版本 PHP 有效,但现在不起作用。
我到底错过了什么?感谢您的帮助。
史蒂夫
编辑:我意识到我可以将 json_decode 简单地更改为 return 一个数组,然后将其作为多维数组处理,但我试图避免重写一堆代码.我最终要处理 JSON 响应中的数百个对象属性,而我遇到的唯一问题是作为对象数组的属性。
编辑 2:由于它的大小,我不愿意添加它,但为了回应评论,这是我正在处理的 JSON。我不想尝试减少它,因为害怕无意中删除导致我出现问题的任何内容。如果有更好的方法来分享大量未受干扰的JSON,请告诉我。
编辑 3:为了回应建筑阵列不在 JSON 中的评论,我将下面的 JSON 从该站点剪切并粘贴到 https://jsonformatter.org/json-parser 中并查看在树视图中的 JSON。我需要通过索引访问的建筑数组其实就在这个JSON.
{"success":true,"status":200,"bundle":[{"landUseDescription":"Single Family Residential","zoningDescription":null,"year":2016,"marketLandValue":26930,"numberOfBuildings":1,"lotSizeIrregular":"85 X 138","county":"Butler County","pools":[],"censusTract":"390170118002005","areas":[{"areaSquareFeet":1492,"type":"Living Building Area"},{"areaSquareFeet":1144,"type":"Base Building Area"},{"areaSquareFeet":1140,"type":"Adjusted Building Area"},{"areaSquareFeet":1492,"type":"Zillow Calculated Finished Area"},{"areaSquareFeet":348,"type":"Addition Area"}],"building":[{"airConditioning":"Central","foundation":null,"architecturalStyle":"Ranch/Rambler","yearBuilt":1965,"fireplace":null,"noOfUnits":1,"effectiveYearBuilt":null,"sewer":"Municipal/Public","heating":"Central","class":null,"comments":null,"quarterBaths":null,"totalStories":1,"kitchens":2,"water":"Municipal/Public","classDescription":null,"quality":"C-","totalRooms":5,"bedrooms":3,"condition":"Average","baths":null,"threeQuarterBaths":null,"halfBaths":null,"occupancyStatus":null,"fullBaths":1}],"lotSizeFrontage":null,"parcelID":"112166032","marketValueYear":2014,"lotTopography":"Rocky","ownerName":["ROBERT D SUTTON"],"taxYear":2016,"marketTotalValue":89400,"taxID":"458467","taxExemption":null,"legal":{"phase":null,"tract":null,"city":null,"municipality":null,"section":null,"lot":null,"otherLot":null,"unit":null,"lotDescription":"1141 ENT","district":null,"block":null,"subdivisionName":null,"township":null},"id":"112166032-2016","state":"OH","lotSizeDepth":null,"apn":"R8000-017.000-071","marketImprovementValue":62470,"totalValue":31290,"address":{"zip":"45067","city":"Trenton","zip4":"1117","streetPre":null,"house":"431","unitType":null,"unit":null,"street":"Kenwood","houseExt":null,"streetSuffix":"Ln","state":"OH","streetPost":null,"full":" 431 Kenwood Ln Trenton OH 45067"},"landUseCode":"RR101","landUseGeneral":"RR","coordinates":[-84.465074,39.489885],"zpid":"33237764","fips":"39017","landValue":9430,"zoningCode":null,"lotSizeAcres":0.27,"garages":[],"improvementValue":21860,"taxAmount":1701.78,"lotSizeSquareFeet":11761.2,"url":"api.bridgedataoutput.com/api/v2/pub/assessments/112166032-2016"},{"landUseDescription":"Single Family Residential","zoningDescription":null,"year":2008,"marketLandValue":29380,"numberOfBuildings":1,"lotSizeIrregular":"85 X 138","county":"Butler County","pools":[],"censusTract":"390170118002005","areas":[{"areaSquareFeet":1492,"type":"Living Building Area"},{"areaSquareFeet":1144,"type":"Base Building Area"},{"areaSquareFeet":1140,"type":"Adjusted Building Area"},{"areaSquareFeet":1492,"type":"Zillow Calculated Finished Area"},{"areaSquareFeet":348,"type":"Addition Area"}],"building":[{"airConditioning":"Central","foundation":null,"architecturalStyle":"Ranch/Rambler","yearBuilt":1965,"fireplace":null,"noOfUnits":1,"effectiveYearBuilt":null,"sewer":"Municipal/Public","heating":"Central","class":null,"comments":null,"quarterBaths":null,"totalStories":1,"kitchens":2,"water":"Municipal/Public","classDescription":null,"quality":"C-","totalRooms":5,"bedrooms":3,"condition":"Average","baths":null,"threeQuarterBaths":null,"halfBaths":null,"occupancyStatus":null,"fullBaths":1}],"lotSizeFrontage":null,"parcelID":"112166032","marketValueYear":2008,"lotTopography":"Rocky","ownerName":["ROBERT D SUTTON"],"taxYear":2008,"marketTotalValue":112120,"taxID":"320818","taxExemption":null,"legal":{"phase":null,"tract":null,"city":null,"municipality":null,"section":null,"lot":null,"otherLot":null,"unit":null,"lotDescription":"1141 ENT","district":null,"block":null,"subdivisionName":null,"township":null},"id":"112166032-2008","state":"OH","lotSizeDepth":null,"apn":"R8000-017.000-071","marketImprovementValue":82740,"totalValue":39242,"address":{"zip":"45067","city":"Trenton","zip4":"1117","streetPre":null,"house":"431","unitType":null,"unit":null,"street":"Kenwood","houseExt":null,"streetSuffix":"Ln","state":"OH","streetPost":null,"full":" 431 Kenwood Ln Trenton OH 45067"},"landUseCode":"RR101","landUseGeneral":"RR","coordinates":[-84.465074,39.489885],"zpid":"33237764","fips":"39017","landValue":10283,"zoningCode":null,"lotSizeAcres":0.27,"garages":[],"improvementValue":28959,"taxAmount":1143.66,"lotSizeSquareFeet":11761.2,"url":"api.bridgedataoutput.com/api/v2/pub/assessments/112166032-2008"},{"landUseDescription":"Single Family Residential","zoningDescription":null,"year":2005,"marketLandValue":24950,"numberOfBuildings":1,"lotSizeIrregular":"85 X 138","county":"Butler County","pools":[],"censusTract":"390170118002005","areas":[{"areaSquareFeet":1492,"type":"Living Building Area"},{"areaSquareFeet":1144,"type":"Base Building Area"},{"areaSquareFeet":1140,"type":"Adjusted Building Area"},{"areaSquareFeet":1492,"type":"Zillow Calculated Finished Area"},{"areaSquareFeet":348,"type":"Addition Area"}],"building":[{"airConditioning":"Central","foundation":null,"architecturalStyle":"Ranch/Rambler","yearBuilt":1965,"fireplace":null,"noOfUnits":1,"effectiveYearBuilt":null,"sewer":"Municipal/Public","heating":"Central","class":null,"comments":null,"quarterBaths":null,"totalStories":1,"kitchens":2,"water":"Municipal/Public","classDescription":null,"quality":"C-","totalRooms":5,"bedrooms":3,"condition":"Average","baths":null,"threeQuarterBaths":null,"halfBaths":null,"occupancyStatus":null,"fullBaths":1}],"lotSizeFrontage":null,"parcelID":"112166032","marketValueYear":2005,"lotTopography":"Rocky","ownerName":["ROBERT D SUTTON"],"taxYear":2005,"marketTotalValue":107700,"taxID":null,"taxExemption":null,"legal":{"phase":null,"tract":null,"city":null,"municipality":null,"section":null,"lot":null,"otherLot":null,"unit":null,"lotDescription":"1141 ENT","district":null,"block":null,"subdivisionName":null,"township":null},"id":"112166032-2005","state":"OH","lotSizeDepth":null,"apn":"R8000-017.000-071","marketImprovementValue":82750,"totalValue":37696,"address":{"zip":"45067","city":"Trenton","zip4":"1117","streetPre":null,"house":"431","unitType":null,"unit":null,"street":"Kenwood","houseExt":null,"streetSuffix":"Ln","state":"OH","streetPost":null,"full":" 431 Kenwood Ln Trenton OH 45067"},"landUseCode":"RR101","landUseGeneral":"RR","coordinates":[-84.465074,39.489885],"zpid":"33237764","fips":"39017","landValue":8733,"zoningCode":null,"lotSizeAcres":0.27,"garages":[],"improvementValue":28963,"taxAmount":1631.24,"lotSizeSquareFeet":11761.2,"url":"api.bridgedataoutput.com/api/v2/pub/assessments/112166032-2005"},{"landUseDescription":"Single Family Residential","zoningDescription":null,"year":2002,"marketLandValue":23760,"numberOfBuildings":1,"lotSizeIrregular":"85 X 138","county":"Butler County","pools":[],"censusTract":"390170118002005","areas":[{"areaSquareFeet":1492,"type":"Living Building Area"},{"areaSquareFeet":1144,"type":"Base Building Area"},{"areaSquareFeet":1140,"type":"Adjusted Building Area"},{"areaSquareFeet":1492,"type":"Zillow Calculated Finished Area"},{"areaSquareFeet":348,"type":"Addition Area"}],"building":[{"airConditioning":"Central","foundation":null,"architecturalStyle":"Ranch/Rambler","yearBuilt":1965,"fireplace":null,"noOfUnits":1,"effectiveYearBuilt":null,"sewer":"Municipal/Public","heating":"Central","class":null,"comments":null,"quarterBaths":null,"totalStories":1,"kitchens":2,"water":"Municipal/Public","classDescription":null,"quality":"C-","totalRooms":5,"bedrooms":3,"condition":"Average","baths":null,"threeQuarterBaths":null,"halfBaths":null,"occupancyStatus":null,"fullBaths":1}],"lotSizeFrontage":null,"parcelID":"112166032","marketValueYear":2002,"lotTopography":"Rocky","ownerName":["ROBERT D SUTTON"],"taxYear":2002,"marketTotalValue":102590,"taxID":null,"taxExemption":null,"legal":{"phase":null,"tract":null,"city":null,"municipality":null,"section":null,"lot":null,"otherLot":null,"unit":null,"lotDescription":"1141 ENT","district":null,"block":null,"subdivisionName":null,"township":null},"id":"112166032-2002","state":"OH","lotSizeDepth":null,"apn":"R8000-017.000-071","marketImprovementValue":78830,"totalValue":35907,"address":{"zip":"45067","city":"Trenton","zip4":"1117","streetPre":null,"house":"431","unitType":null,"unit":null,"street":"Kenwood","houseExt":null,"streetSuffix":"Ln","state":"OH","streetPost":null,"full":" 431 Kenwood Ln Trenton OH 45067"},"landUseCode":"RR101","landUseGeneral":"RR","coordinates":[-84.465074,39.489885],"zpid":"33237764","fips":"39017","landValue":8316,"zoningCode":null,"lotSizeAcres":0.27,"garages":[],"improvementValue":27591,"taxAmount":1352.82,"lotSizeSquareFeet":11761.2,"url":"api.bridgedataoutput.com/api/v2/pub/assessments/112166032-2002"},{"landUseDescription":"Single Family Residential","zoningDescription":null,"year":2011,"marketLandValue":29380,"numberOfBuildings":1,"lotSizeIrregular":"85 X 138","county":"Butler County","pools":[],"censusTract":"390170118002005","areas":[{"areaSquareFeet":1492,"type":"Living Building Area"},{"areaSquareFeet":1144,"type":"Base Building Area"},{"areaSquareFeet":1140,"type":"Adjusted Building Area"},{"areaSquareFeet":1492,"type":"Zillow Calculated Finished Area"},{"areaSquareFeet":348,"type":"Addition Area"}],"building":[{"airConditioning":"Central","foundation":null,"architecturalStyle":"Ranch/Rambler","yearBuilt":1965,"fireplace":null,"noOfUnits":1,"effectiveYearBuilt":null,"sewer":"Municipal/Public","heating":"Central","class":null,"comments":null,"quarterBaths":null,"totalStories":1,"kitchens":2,"water":"Municipal/Public","classDescription":null,"quality":"C-","totalRooms":5,"bedrooms":3,"condition":"Average","baths":null,"threeQuarterBaths":null,"halfBaths":null,"occupancyStatus":null,"fullBaths":1}],"lotSizeFrontage":null,"parcelID":"112166032","marketValueYear":2011,"lotTopography":"Rocky","ownerName":["ROBERT D SUTTON"],"taxYear":2011,"marketTotalValue":103050,"taxID":"60577","taxExemption":null,"legal":{"phase":null,"tract":null,"city":null,"municipality":null,"section":null,"lot":null,"otherLot":null,"unit":null,"lotDescription":"1141 ENT","district":null,"block":null,"subdivisionName":null,"township":null},"id":"112166032-2011","state":"OH","lotSizeDepth":null,"apn":"R8000-017.000-071","marketImprovementValue":73670,"totalValue":36068,"address":{"zip":"45067","city":"Trenton","zip4":"1117","streetPre":null,"house":"431","unitType":null,"unit":null,"street":"Kenwood","houseExt":null,"streetSuffix":"Ln","state":"OH","streetPost":null,"full":" 431 Kenwood Ln Trenton OH 45067"},"landUseCode":"RR101","landUseGeneral":"RR","coordinates":[-84.465074,39.489885],"zpid":"33237764","fips":"39017","landValue":10283,"zoningCode":null,"lotSizeAcres":0.27,"garages":[],"improvementValue":25785,"taxAmount":1591.38,"lotSizeSquareFeet":11761.2,"url":"api.bridgedataoutput.com/api/v2/pub/assessments/112166032-2011"},{"landUseDescription":"Single Family Residential","zoningDescription":null,"year":2010,"marketLandValue":29380,"numberOfBuildings":1,"lotSizeIrregular":"85 X 138","county":"Butler County","pools":[],"censusTract":"390170118002005","areas":[{"areaSquareFeet":1492,"type":"Living Building Area"},{"areaSquareFeet":1144,"type":"Base Building Area"},{"areaSquareFeet":1140,"type":"Adjusted Building Area"},{"areaSquareFeet":1492,"type":"Zillow Calculated Finished Area"},{"areaSquareFeet":348,"type":"Addition Area"}],"building":[{"airConditioning":"Central","foundation":null,"architecturalStyle":"Ranch/Rambler","yearBuilt":1965,"fireplace":null,"noOfUnits":1,"effectiveYearBuilt":null,"sewer":"Municipal/Public","heating":"Central","class":null,"comments":null,"quarterBaths":null,"totalStories":1,"kitchens":2,"water":"Municipal/Public","classDescription":null,"quality":"C-","totalRooms":5,"bedrooms":3,"condition":"Average","baths":null,"threeQuarterBaths":null,"halfBaths":null,"occupancyStatus":null,"fullBaths":1}],"lotSizeFrontage":null,"parcelID":"112166032","marketValueYear":2010,"lotTopography":"Rocky","ownerName":["ROBERT D SUTTON"],"taxYear":2010,"marketTotalValue":105520,"taxID":"475393","taxExemption":null,"legal":{"phase":null,"tract":null,"city":null,"municipality":null,"section":null,"lot":null,"otherLot":null,"unit":null,"lotDescription":"1141 ENT","district":null,"block":null,"subdivisionName":null,"township":null},"id":"112166032-2010","state":"OH","lotSizeDepth":null,"apn":"R8000-017.000-071","marketImprovementValue":76140,"totalValue":36932,"address":{"zip":"45067","city":"Trenton","zip4":"1117","streetPre":null,"house":"431","unitType":null,"unit":null,"street":"Kenwood","houseExt":null,"streetSuffix":"Ln","state":"OH","streetPost":null,"full":" 431 Kenwood Ln Trenton OH 45067"},"landUseCode":"RR101","landUseGeneral":"RR","coordinates":[-84.465074,39.489885],"zpid":"33237764","fips":"39017","landValue":10283,"zoningCode":null,"lotSizeAcres":0.27,"garages":[],"improvementValue":26649,"taxAmount":1159.18,"lotSizeSquareFeet":11761.2,"url":"api.bridgedataoutput.com/api/v2/pub/assessments/112166032-2010"},{"landUseDescription":"Single Family Residential","zoningDescription":null,"year":2007,"marketLandValue":24950,"numberOfBuildings":1,"lotSizeIrregular":"85 X 138","county":"Butler County","pools":[],"censusTract":"390170118002005","areas":[{"areaSquareFeet":1492,"type":"Living Building Area"},{"areaSquareFeet":1144,"type":"Base Building Area"},{"areaSquareFeet":1140,"type":"Adjusted Building Area"},{"areaSquareFeet":1492,"type":"Zillow Calculated Finished Area"},{"areaSquareFeet":348,"type":"Addition Area"}],"building":[{"airConditioning":"Central","foundation":null,"architecturalStyle":"Ranch/Rambler","yearBuilt":1965,"fireplace":null,"noOfUnits":1,"effectiveYearBuilt":null,"sewer":"Municipal/Public","heating":"Central","class":null,"comments":null,"quarterBaths":null,"totalStories":1,"kitchens":2,"water":"Municipal/Public","classDescription":null,"quality":"C-","totalRooms":5,"bedrooms":3,"condition":"Average","baths":null,"threeQuarterBaths":null,"halfBaths":null,"occupancyStatus":null,"fullBaths":1}],"lotSizeFrontage":null,"parcelID":"112166032","marketValueYear":2007,"lotTopography":"Rocky","ownerName":["ROBERT D SUTTON"],"taxYear":2007,"marketTotalValue":107700,"taxID":"61840","taxExemption":null,"legal":{"phase":null,"tract":null,"city":null,"municipality":null,"section":null,"lot":null,"otherLot":null,"unit":null,"lotDescription":"1141 ENT","district":null,"block":null,"subdivisionName":null,"township":null},"id":"112166032-2007","state":"OH","lotSizeDepth":null,"apn":"R8000-017.000-071","marketImprovementValue":82750,"totalValue":37696,"address":{"zip":"45067","city":"Trenton","zip4":"1117","streetPre":null,"house":"431","unitType":null,"unit":null,"street":"Kenwood","houseExt":null,"streetSuffix":"Ln","state":"OH","streetPost":null,"full":" 431 Kenwood Ln Trenton OH 45067"},"landUseCode":"RR101","landUseGeneral":"RR","coordinates":[-84.465074,39.489885],"zpid":"33237764","fips":"39017","landValue":8733,"zoningCode":null,"lotSizeAcres":0.27,"garages":[],"improvementValue":28963,"taxAmount":1258.38,"lotSizeSquareFeet":11761.2,"url":"api.bridgedataoutput.com/api/v2/pub/assessments/112166032-2007"},{"landUseDescription":"Single Family Residential","zoningDescription":null,"year":2014,"marketLandValue":26930,"numberOfBuildings":1,"lotSizeIrregular":"85 X 138","county":"Butler County","pools":[],"censusTract":"390170118002005","areas":[{"areaSquareFeet":1492,"type":"Living Building Area"},{"areaSquareFeet":1144,"type":"Base Building Area"},{"areaSquareFeet":1140,"type":"Adjusted Building Area"},{"areaSquareFeet":1492,"type":"Zillow Calculated Finished Area"},{"areaSquareFeet":348,"type":"Addition Area"}],"building":[{"airConditioning":"Central","foundation":null,"architecturalStyle":"Ranch/Rambler","yearBuilt":1965,"fireplace":null,"noOfUnits":1,"effectiveYearBuilt":null,"sewer":"Municipal/Public","heating":"Central","class":null,"comments":null,"quarterBaths":null,"totalStories":1,"kitchens":2,"water":"Municipal/Public","classDescription":null,"quality":"C-","totalRooms":5,"bedrooms":3,"condition":"Average","baths":null,"threeQuarterBaths":null,"halfBaths":null,"occupancyStatus":null,"fullBaths":1}],"lotSizeFrontage":null,"parcelID":"112166032","marketValueYear":2014,"lotTopography":"Rocky","ownerName":["ROBERT D SUTTON"],"taxYear":2013,"marketTotalValue":89400,"taxID":"121009","taxExemption":null,"legal":{"phase":null,"tract":null,"city":null,"municipality":null,"section":null,"lot":null,"otherLot":null,"unit":null,"lotDescription":"1141 ENT","district":null,"block":null,"subdivisionName":null,"township":null},"id":"112166032-2014","state":"OH","lotSizeDepth":null,"apn":"R8000-017.000-071","marketImprovementValue":62470,"totalValue":31290,"address":{"zip":"45067","city":"Trenton","zip4":"1117","streetPre":null,"house":"431","unitType":null,"unit":null,"street":"Kenwood","houseExt":null,"streetSuffix":"Ln","state":"OH","streetPost":null,"full":" 431 Kenwood Ln Trenton OH 45067"},"landUseCode":"RR101","landUseGeneral":"RR","coordinates":[-84.465074,39.489885],"zpid":"33237764","fips":"39017","landValue":9430,"zoningCode":null,"lotSizeAcres":0.27,"garages":[],"improvementValue":21860,"taxAmount":1678.56,"lotSizeSquareFeet":11761.2,"url":"api.bridgedataoutput.com/api/v2/pub/assessments/112166032-2014"},{"landUseDescription":"Single Family Residential","zoningDescription":null,"year":2003,"marketLandValue":23760,"numberOfBuildings":1,"lotSizeIrregular":"85 X 138","county":"Butler County","pools":[],"censusTract":"390170118002005","areas":[{"areaSquareFeet":1492,"type":"Living Building Area"},{"areaSquareFeet":1144,"type":"Base Building Area"},{"areaSquareFeet":1140,"type":"Adjusted Building Area"},{"areaSquareFeet":1492,"type":"Zillow Calculated Finished Area"},{"areaSquareFeet":348,"type":"Addition Area"}],"building":[{"airConditioning":"Central","foundation":null,"architecturalStyle":"Ranch/Rambler","yearBuilt":1965,"fireplace":null,"noOfUnits":1,"effectiveYearBuilt":null,"sewer":"Municipal/Public","heating":"Central","class":null,"comments":null,"quarterBaths":null,"totalStories":1,"kitchens":2,"water":"Municipal/Public","classDescription":null,"quality":"C-","totalRooms":5,"bedrooms":3,"condition":"Average","baths":null,"threeQuarterBaths":null,"halfBaths":null,"occupancyStatus":null,"fullBaths":1}],"lotSizeFrontage":null,"parcelID":"112166032","marketValueYear":2003,"lotTopography":"Rocky","ownerName":["ROBERT D SUTTON"],"taxYear":2003,"marketTotalValue":102590,"taxID":null,"taxExemption":null,"legal":{"phase":null,"tract":null,"city":null,"municipality":null,"section":null,"lot":null,"otherLot":null,"unit":null,"lotDescription":"1141 ENT","district":null,"block":null,"subdivisionName":null,"township":null},"id":"112166032-2003","state":"OH","lotSizeDepth":null,"apn":"R8000-017.000-071","marketImprovementValue":78830,"totalValue":35907,"address":{"zip":"45067","city":"Trenton","zip4":"1117","streetPre":null,"house":"431","unitType":null,"unit":null,"street":"Kenwood","houseExt":null,"streetSuffix":"Ln","state":"OH","streetPost":null,"full":" 431 Kenwood Ln Trenton OH 45067"},"landUseCode":"RR101","landUseGeneral":"RR","coordinates":[-84.465074,39.489885],"zpid":"33237764","fips":"39017","landValue":8316,"zoningCode":null,"lotSizeAcres":0.27,"garages":[],"improvementValue":27591,"taxAmount":1380.02,"lotSizeSquareFeet":11761.2,"url":"api.bridgedataoutput.com/api/v2/pub/assessments/112166032-2003"},{"landUseDescription":"Single Family Residential","zoningDescription":null,"year":2009,"marketLandValue":29380,"numberOfBuildings":1,"lotSizeIrregular":"85 X 138","county":"Butler County","pools":[],"censusTract":"390170118002005","areas":[{"areaSquareFeet":1492,"type":"Living Building Area"},{"areaSquareFeet":1144,"type":"Base Building Area"},{"areaSquareFeet":1140,"type":"Adjusted Building Area"},{"areaSquareFeet":1492,"type":"Zillow Calculated Finished Area"},{"areaSquareFeet":348,"type":"Addition Area"}],"building":[{"airConditioning":"Central","foundation":null,"architecturalStyle":"Ranch/Rambler","yearBuilt":1965,"fireplace":null,"noOfUnits":1,"effectiveYearBuilt":null,"sewer":"Municipal/Public","heating":"Central","class":null,"comments":null,"quarterBaths":null,"totalStories":1,"kitchens":2,"water":"Municipal/Public","classDescription":null,"quality":"C-","totalRooms":5,"bedrooms":3,"condition":"Average","baths":null,"threeQuarterBaths":null,"halfBaths":null,"occupancyStatus":null,"fullBaths":1}],"lotSizeFrontage":null,"parcelID":"112166032","marketValueYear":2009,"lotTopography":"Rocky","ownerName":["ROBERT D SUTTON"],"taxYear":2009,"marketTotalValue":105520,"taxID":"478717","taxExemption":null,"legal":{"phase":null,"tract":null,"city":null,"municipality":null,"section":null,"lot":null,"otherLot":null,"unit":null,"lotDescription":"1141 ENT","district":null,"block":null,"subdivisionName":null,"township":null},"id":"112166032-2009","state":"OH","lotSizeDepth":null,"apn":"R8000-017.000-071","marketImprovementValue":76140,"totalValue":36932,"address":{"zip":"45067","city":"Trenton","zip4":"1117","streetPre":null,"house":"431","unitType":null,"unit":null,"street":"Kenwood","houseExt":null,"streetSuffix":"Ln","state":"OH","streetPost":null,"full":" 431 Kenwood Ln Trenton OH 45067"},"landUseCode":"RR101","landUseGeneral":"RR","coordinates":[-84.465074,39.489885],"zpid":"33237764","fips":"39017","landValue":10283,"zoningCode":null,"lotSizeAcres":0.27,"garages":[],"improvementValue":26649,"taxAmount":1154,"lotSizeSquareFeet":11761.2,"url":"api.bridgedataoutput.com/api/v2/pub/assessments/112166032-2009"}],"total":21}
将 JSON 解码为 php 是一个复杂的操作:它是 Javascript 马和 php 驴的杂交。所以我认为它可能会有点混乱,如果不是固执的话。
这里有两种方法可以将埋在 JSON 对象中的平方英尺相加。
/* php objects */
$ResponseInfo = json_decode( $json, false );
$sqft = 0;
foreach ( $ResponseInfo->bundle as $bundle ) {
foreach ( $bundle->areas as $area ) {
$sqft += $area->areaSquareFeet;
}
}
/* php traditional associative arrays */
$ResponseInfo = json_decode( $json, true );
$sqft = 0;
foreach ( $ResponseInfo['bundle'] as $bundle ) {
foreach ( $bundle['areas'] as $area ) {
$sqft += $area['areaSquareFeet'];
}
}
在任何一种情况下,当您从 json_decode
的 $ResponseInfo
输出开始时,您必须明确地从该对象的顶层导航到您需要的数据。这种表达方式不行。
$sqft = $ResponseInfo->building[0]->squarefeet;
building
不是该解码对象的成员,并且在您的 JSON.
中没有名为 squarefeet
的成员
编辑
此表达式访问 JSON 中第一个 bundle 对象中的第一个建筑对象(当解码为 php 对象时)。
$ResponseInfo->bundle[0]->building[0]->airConditioning;
如您所知,API 响应的有效负载位于名为 bundle
的数组中。 (top-level 响应中的其他内容是显示请求状态的 API 包装器。)所以上面的表达式查看 bundle 数组的第一个元素以找到构建数组。然后它查看建筑数组的第一个元素以找到名为 noOfUnits
.
的对象 属性
我应该解释一下我做了什么——我是如何得出答案的。我有启用了 JSON 格式化程序插件的记事本++。我将您的 JSON 放入该编辑器的草稿文档中并对其进行了格式化。然后逐层探索您的 JSON 对象变得容易。
php 对象有一个不幸的 属性 引用 $object->undefined
returns null 而不是抛出错误。空值上的 foreach()
只循环零次。
这个 post 是一个狗屎节目,所以很抱歉,如果我只是浏览了一下,请使用代码块,这样我们就可以真正解决问题,而不是在里面乱扔一堆数据摆在我们面前的,很可能对手头的问题毫无用处。
您好像在使用 stdClass Object
,这可能会让您失望(目前只是猜测)。
如果您想将数据完全转换为 stdClass 对象或完全转换为数组以便于管理;您可以使用以下任一功能:
function array_to_stdClass(&$array){
$tmp = $array;
$array = new stdClass();
foreach($tmp as $k => $v) $array->$k = (!is_object($v) && !is_array($v)) ? $v:array_to_stdClass($v);
return $array;
}
function stdClass_to_array(&$stdClass){
$tmp = $stdClass;
$stdClass = array();
foreach($tmp as $k => $v) $stdClass[$k] = (!is_object($v) && !is_array($v)) ? $v:stdClass_to_array($v);
return $stdClass;
}
在这里直播 Demo/Example:https://onlinephp.io/c/2d00a
嗯。我是个白痴。感谢所有看过的人。对不起,我浪费了你的时间。
访问我需要的数据的正确方法实际上是:
$ResponseInfo->bundle[0]->building[0]->airConditioning;
当然调整为正确的变量和成员。我遇到问题的原因是我有狭隘的视野,当我试图将我的代码减少到最简单的元素时,我没有完全做到。
特别是,我试图调试的代码在循环中被反复调用以提取 属性 信息,当我简化事情时我忘记了这一点。我的测试是提取五个属性的数据,我专注于第一个,它的数据是正确的,并且经过我和其他人的反复验证。
然而,循环中的第二个 属性 并没有发挥得那么好。事实证明,为 属性 返回的 JSON 数据实际上缺少我需要的元素。那当然是完全不同的问题了。
仅仅因为抛出了一个错误,并不一定意味着它是循环的第一次迭代导致的,而我忽略了这一点。尽管我正在处理的代码是如此模块化,但我的克星循环已从我关注的例程中完全删除。
我可能应该刚刚删除了这个问题,但我认为那些花时间查看它的人会很感激跟进,也许它会帮助其他人在进行故障排除时考虑更大的前景。
再次感谢大家。
史蒂夫
简而言之,我有一个对象数组,它是从网络服务的 JSON 响应中解码出来的,尽管我尽了最大努力,但我无法通过索引访问该数组中的对象。
我专门解码响应:
$ResponseInfo = json_decode($ResponseBody, false); //give to me as an object
在访问作为对象数组的 属性 之前,我可以毫无问题地访问结果对象的属性。对于上下文,它是 属性 包含一栋或多栋建筑物详细信息的信息。
我希望能够通过索引访问建筑物的详细信息,例如:
$ac = $ResponseInfo->building[0]->airConditioning;
这不起作用。我得到一个未定义的索引 0。奇怪的是,array_keys 给我:
数组([0] => 0)
看来 index 方法应该有用,而且它不是什么奇怪的键,但可惜它没有。
A print_r 给我这个:
Array ( [0] => stdClass Object ( [airConditioning] => Central [foundation] => [architecturalStyle] => Ranch/Rambler [yearBuilt] => 1965 [fireplace] => [noOfUnits] => 1 [effectiveYearBuilt] => [sewer] => Municipal/Public [heating] => Central [class] => [comments] => [quarterBaths] => [totalStories] => 1 [kitchens] => 2 [water] => Municipal/Public [classDescription] => [quality] => C- [totalRooms] => 5 [bedrooms] => 3 [condition] => Average [baths] => [threeQuarterBaths] => [halfBaths] => [occupancyStatus] => [fullBaths] => 1 ) ) Array ( [0] => BuildingInformationObject Object ( [airConditioning] => Central [foundation] => [architecturalStyle] => Ranch/Rambler [yearBuilt] => 1965 [fireplace] => [noOfUnits] => 1 [sewer] => Municipal/Public [heating] => Central [totalStories] => 1 [kitchens] => 2 [water] => Municipal/Public [totalRooms] => 5 [bedrooms] => 3 [condition] => Average [halfBaths] => [occupancyStatus] => [fullBaths] => 1 ) )
我也试过将数组分配给一个新变量,但我仍然得到未定义的索引 0。
$BuildingInfo = $ResponseInfo->building;
奇怪的是,起作用的是:
foreach ($BuildingInfo as $ThisBuilding) {
$ThisBuilding-airconditioning(或其他 属性)以这种方式运行良好。所以我什至尝试创建一个全新的数组并手动分配各个属性,如下所示:
$Buildings = array();
$i = 0;
foreach ($BuildingInfo as $ThisBuilding)
{
$Buildings[$i] = new BuildingInformationObject();
//$Buildings[$i] = $ThisBuilding; //just assigning variable didn't work, so copied properties manually
$Buildings[$i]->airConditioning = $ThisBuilding->airConditioning;
$Buildings[$i]->foundation = $ThisBuilding->foundation;
$Buildings[$i]->architecturalStyle = $ThisBuilding->architecturalStyle;
等..
毕竟,尝试 $Buildings[0]->airConditioning 仍然给我可怕的索引 0 undefined,即使在新阵列上也是如此。
我什至尝试过
形式的东西echo $data->{'0'}->UserName;
如:
$BuildingInfo->{'0'}->airConditioning;
我在 SO 上找到的。此方法显然对旧版本 PHP 有效,但现在不起作用。
我到底错过了什么?感谢您的帮助。
史蒂夫
编辑:我意识到我可以将 json_decode 简单地更改为 return 一个数组,然后将其作为多维数组处理,但我试图避免重写一堆代码.我最终要处理 JSON 响应中的数百个对象属性,而我遇到的唯一问题是作为对象数组的属性。
编辑 2:由于它的大小,我不愿意添加它,但为了回应评论,这是我正在处理的 JSON。我不想尝试减少它,因为害怕无意中删除导致我出现问题的任何内容。如果有更好的方法来分享大量未受干扰的JSON,请告诉我。
编辑 3:为了回应建筑阵列不在 JSON 中的评论,我将下面的 JSON 从该站点剪切并粘贴到 https://jsonformatter.org/json-parser 中并查看在树视图中的 JSON。我需要通过索引访问的建筑数组其实就在这个JSON.
{"success":true,"status":200,"bundle":[{"landUseDescription":"Single Family Residential","zoningDescription":null,"year":2016,"marketLandValue":26930,"numberOfBuildings":1,"lotSizeIrregular":"85 X 138","county":"Butler County","pools":[],"censusTract":"390170118002005","areas":[{"areaSquareFeet":1492,"type":"Living Building Area"},{"areaSquareFeet":1144,"type":"Base Building Area"},{"areaSquareFeet":1140,"type":"Adjusted Building Area"},{"areaSquareFeet":1492,"type":"Zillow Calculated Finished Area"},{"areaSquareFeet":348,"type":"Addition Area"}],"building":[{"airConditioning":"Central","foundation":null,"architecturalStyle":"Ranch/Rambler","yearBuilt":1965,"fireplace":null,"noOfUnits":1,"effectiveYearBuilt":null,"sewer":"Municipal/Public","heating":"Central","class":null,"comments":null,"quarterBaths":null,"totalStories":1,"kitchens":2,"water":"Municipal/Public","classDescription":null,"quality":"C-","totalRooms":5,"bedrooms":3,"condition":"Average","baths":null,"threeQuarterBaths":null,"halfBaths":null,"occupancyStatus":null,"fullBaths":1}],"lotSizeFrontage":null,"parcelID":"112166032","marketValueYear":2014,"lotTopography":"Rocky","ownerName":["ROBERT D SUTTON"],"taxYear":2016,"marketTotalValue":89400,"taxID":"458467","taxExemption":null,"legal":{"phase":null,"tract":null,"city":null,"municipality":null,"section":null,"lot":null,"otherLot":null,"unit":null,"lotDescription":"1141 ENT","district":null,"block":null,"subdivisionName":null,"township":null},"id":"112166032-2016","state":"OH","lotSizeDepth":null,"apn":"R8000-017.000-071","marketImprovementValue":62470,"totalValue":31290,"address":{"zip":"45067","city":"Trenton","zip4":"1117","streetPre":null,"house":"431","unitType":null,"unit":null,"street":"Kenwood","houseExt":null,"streetSuffix":"Ln","state":"OH","streetPost":null,"full":" 431 Kenwood Ln Trenton OH 45067"},"landUseCode":"RR101","landUseGeneral":"RR","coordinates":[-84.465074,39.489885],"zpid":"33237764","fips":"39017","landValue":9430,"zoningCode":null,"lotSizeAcres":0.27,"garages":[],"improvementValue":21860,"taxAmount":1701.78,"lotSizeSquareFeet":11761.2,"url":"api.bridgedataoutput.com/api/v2/pub/assessments/112166032-2016"},{"landUseDescription":"Single Family Residential","zoningDescription":null,"year":2008,"marketLandValue":29380,"numberOfBuildings":1,"lotSizeIrregular":"85 X 138","county":"Butler County","pools":[],"censusTract":"390170118002005","areas":[{"areaSquareFeet":1492,"type":"Living Building Area"},{"areaSquareFeet":1144,"type":"Base Building Area"},{"areaSquareFeet":1140,"type":"Adjusted Building Area"},{"areaSquareFeet":1492,"type":"Zillow Calculated Finished Area"},{"areaSquareFeet":348,"type":"Addition Area"}],"building":[{"airConditioning":"Central","foundation":null,"architecturalStyle":"Ranch/Rambler","yearBuilt":1965,"fireplace":null,"noOfUnits":1,"effectiveYearBuilt":null,"sewer":"Municipal/Public","heating":"Central","class":null,"comments":null,"quarterBaths":null,"totalStories":1,"kitchens":2,"water":"Municipal/Public","classDescription":null,"quality":"C-","totalRooms":5,"bedrooms":3,"condition":"Average","baths":null,"threeQuarterBaths":null,"halfBaths":null,"occupancyStatus":null,"fullBaths":1}],"lotSizeFrontage":null,"parcelID":"112166032","marketValueYear":2008,"lotTopography":"Rocky","ownerName":["ROBERT D SUTTON"],"taxYear":2008,"marketTotalValue":112120,"taxID":"320818","taxExemption":null,"legal":{"phase":null,"tract":null,"city":null,"municipality":null,"section":null,"lot":null,"otherLot":null,"unit":null,"lotDescription":"1141 ENT","district":null,"block":null,"subdivisionName":null,"township":null},"id":"112166032-2008","state":"OH","lotSizeDepth":null,"apn":"R8000-017.000-071","marketImprovementValue":82740,"totalValue":39242,"address":{"zip":"45067","city":"Trenton","zip4":"1117","streetPre":null,"house":"431","unitType":null,"unit":null,"street":"Kenwood","houseExt":null,"streetSuffix":"Ln","state":"OH","streetPost":null,"full":" 431 Kenwood Ln Trenton OH 45067"},"landUseCode":"RR101","landUseGeneral":"RR","coordinates":[-84.465074,39.489885],"zpid":"33237764","fips":"39017","landValue":10283,"zoningCode":null,"lotSizeAcres":0.27,"garages":[],"improvementValue":28959,"taxAmount":1143.66,"lotSizeSquareFeet":11761.2,"url":"api.bridgedataoutput.com/api/v2/pub/assessments/112166032-2008"},{"landUseDescription":"Single Family Residential","zoningDescription":null,"year":2005,"marketLandValue":24950,"numberOfBuildings":1,"lotSizeIrregular":"85 X 138","county":"Butler County","pools":[],"censusTract":"390170118002005","areas":[{"areaSquareFeet":1492,"type":"Living Building Area"},{"areaSquareFeet":1144,"type":"Base Building Area"},{"areaSquareFeet":1140,"type":"Adjusted Building Area"},{"areaSquareFeet":1492,"type":"Zillow Calculated Finished Area"},{"areaSquareFeet":348,"type":"Addition Area"}],"building":[{"airConditioning":"Central","foundation":null,"architecturalStyle":"Ranch/Rambler","yearBuilt":1965,"fireplace":null,"noOfUnits":1,"effectiveYearBuilt":null,"sewer":"Municipal/Public","heating":"Central","class":null,"comments":null,"quarterBaths":null,"totalStories":1,"kitchens":2,"water":"Municipal/Public","classDescription":null,"quality":"C-","totalRooms":5,"bedrooms":3,"condition":"Average","baths":null,"threeQuarterBaths":null,"halfBaths":null,"occupancyStatus":null,"fullBaths":1}],"lotSizeFrontage":null,"parcelID":"112166032","marketValueYear":2005,"lotTopography":"Rocky","ownerName":["ROBERT D SUTTON"],"taxYear":2005,"marketTotalValue":107700,"taxID":null,"taxExemption":null,"legal":{"phase":null,"tract":null,"city":null,"municipality":null,"section":null,"lot":null,"otherLot":null,"unit":null,"lotDescription":"1141 ENT","district":null,"block":null,"subdivisionName":null,"township":null},"id":"112166032-2005","state":"OH","lotSizeDepth":null,"apn":"R8000-017.000-071","marketImprovementValue":82750,"totalValue":37696,"address":{"zip":"45067","city":"Trenton","zip4":"1117","streetPre":null,"house":"431","unitType":null,"unit":null,"street":"Kenwood","houseExt":null,"streetSuffix":"Ln","state":"OH","streetPost":null,"full":" 431 Kenwood Ln Trenton OH 45067"},"landUseCode":"RR101","landUseGeneral":"RR","coordinates":[-84.465074,39.489885],"zpid":"33237764","fips":"39017","landValue":8733,"zoningCode":null,"lotSizeAcres":0.27,"garages":[],"improvementValue":28963,"taxAmount":1631.24,"lotSizeSquareFeet":11761.2,"url":"api.bridgedataoutput.com/api/v2/pub/assessments/112166032-2005"},{"landUseDescription":"Single Family Residential","zoningDescription":null,"year":2002,"marketLandValue":23760,"numberOfBuildings":1,"lotSizeIrregular":"85 X 138","county":"Butler County","pools":[],"censusTract":"390170118002005","areas":[{"areaSquareFeet":1492,"type":"Living Building Area"},{"areaSquareFeet":1144,"type":"Base Building Area"},{"areaSquareFeet":1140,"type":"Adjusted Building Area"},{"areaSquareFeet":1492,"type":"Zillow Calculated Finished Area"},{"areaSquareFeet":348,"type":"Addition Area"}],"building":[{"airConditioning":"Central","foundation":null,"architecturalStyle":"Ranch/Rambler","yearBuilt":1965,"fireplace":null,"noOfUnits":1,"effectiveYearBuilt":null,"sewer":"Municipal/Public","heating":"Central","class":null,"comments":null,"quarterBaths":null,"totalStories":1,"kitchens":2,"water":"Municipal/Public","classDescription":null,"quality":"C-","totalRooms":5,"bedrooms":3,"condition":"Average","baths":null,"threeQuarterBaths":null,"halfBaths":null,"occupancyStatus":null,"fullBaths":1}],"lotSizeFrontage":null,"parcelID":"112166032","marketValueYear":2002,"lotTopography":"Rocky","ownerName":["ROBERT D SUTTON"],"taxYear":2002,"marketTotalValue":102590,"taxID":null,"taxExemption":null,"legal":{"phase":null,"tract":null,"city":null,"municipality":null,"section":null,"lot":null,"otherLot":null,"unit":null,"lotDescription":"1141 ENT","district":null,"block":null,"subdivisionName":null,"township":null},"id":"112166032-2002","state":"OH","lotSizeDepth":null,"apn":"R8000-017.000-071","marketImprovementValue":78830,"totalValue":35907,"address":{"zip":"45067","city":"Trenton","zip4":"1117","streetPre":null,"house":"431","unitType":null,"unit":null,"street":"Kenwood","houseExt":null,"streetSuffix":"Ln","state":"OH","streetPost":null,"full":" 431 Kenwood Ln Trenton OH 45067"},"landUseCode":"RR101","landUseGeneral":"RR","coordinates":[-84.465074,39.489885],"zpid":"33237764","fips":"39017","landValue":8316,"zoningCode":null,"lotSizeAcres":0.27,"garages":[],"improvementValue":27591,"taxAmount":1352.82,"lotSizeSquareFeet":11761.2,"url":"api.bridgedataoutput.com/api/v2/pub/assessments/112166032-2002"},{"landUseDescription":"Single Family Residential","zoningDescription":null,"year":2011,"marketLandValue":29380,"numberOfBuildings":1,"lotSizeIrregular":"85 X 138","county":"Butler County","pools":[],"censusTract":"390170118002005","areas":[{"areaSquareFeet":1492,"type":"Living Building Area"},{"areaSquareFeet":1144,"type":"Base Building Area"},{"areaSquareFeet":1140,"type":"Adjusted Building Area"},{"areaSquareFeet":1492,"type":"Zillow Calculated Finished Area"},{"areaSquareFeet":348,"type":"Addition Area"}],"building":[{"airConditioning":"Central","foundation":null,"architecturalStyle":"Ranch/Rambler","yearBuilt":1965,"fireplace":null,"noOfUnits":1,"effectiveYearBuilt":null,"sewer":"Municipal/Public","heating":"Central","class":null,"comments":null,"quarterBaths":null,"totalStories":1,"kitchens":2,"water":"Municipal/Public","classDescription":null,"quality":"C-","totalRooms":5,"bedrooms":3,"condition":"Average","baths":null,"threeQuarterBaths":null,"halfBaths":null,"occupancyStatus":null,"fullBaths":1}],"lotSizeFrontage":null,"parcelID":"112166032","marketValueYear":2011,"lotTopography":"Rocky","ownerName":["ROBERT D SUTTON"],"taxYear":2011,"marketTotalValue":103050,"taxID":"60577","taxExemption":null,"legal":{"phase":null,"tract":null,"city":null,"municipality":null,"section":null,"lot":null,"otherLot":null,"unit":null,"lotDescription":"1141 ENT","district":null,"block":null,"subdivisionName":null,"township":null},"id":"112166032-2011","state":"OH","lotSizeDepth":null,"apn":"R8000-017.000-071","marketImprovementValue":73670,"totalValue":36068,"address":{"zip":"45067","city":"Trenton","zip4":"1117","streetPre":null,"house":"431","unitType":null,"unit":null,"street":"Kenwood","houseExt":null,"streetSuffix":"Ln","state":"OH","streetPost":null,"full":" 431 Kenwood Ln Trenton OH 45067"},"landUseCode":"RR101","landUseGeneral":"RR","coordinates":[-84.465074,39.489885],"zpid":"33237764","fips":"39017","landValue":10283,"zoningCode":null,"lotSizeAcres":0.27,"garages":[],"improvementValue":25785,"taxAmount":1591.38,"lotSizeSquareFeet":11761.2,"url":"api.bridgedataoutput.com/api/v2/pub/assessments/112166032-2011"},{"landUseDescription":"Single Family Residential","zoningDescription":null,"year":2010,"marketLandValue":29380,"numberOfBuildings":1,"lotSizeIrregular":"85 X 138","county":"Butler County","pools":[],"censusTract":"390170118002005","areas":[{"areaSquareFeet":1492,"type":"Living Building Area"},{"areaSquareFeet":1144,"type":"Base Building Area"},{"areaSquareFeet":1140,"type":"Adjusted Building Area"},{"areaSquareFeet":1492,"type":"Zillow Calculated Finished Area"},{"areaSquareFeet":348,"type":"Addition Area"}],"building":[{"airConditioning":"Central","foundation":null,"architecturalStyle":"Ranch/Rambler","yearBuilt":1965,"fireplace":null,"noOfUnits":1,"effectiveYearBuilt":null,"sewer":"Municipal/Public","heating":"Central","class":null,"comments":null,"quarterBaths":null,"totalStories":1,"kitchens":2,"water":"Municipal/Public","classDescription":null,"quality":"C-","totalRooms":5,"bedrooms":3,"condition":"Average","baths":null,"threeQuarterBaths":null,"halfBaths":null,"occupancyStatus":null,"fullBaths":1}],"lotSizeFrontage":null,"parcelID":"112166032","marketValueYear":2010,"lotTopography":"Rocky","ownerName":["ROBERT D SUTTON"],"taxYear":2010,"marketTotalValue":105520,"taxID":"475393","taxExemption":null,"legal":{"phase":null,"tract":null,"city":null,"municipality":null,"section":null,"lot":null,"otherLot":null,"unit":null,"lotDescription":"1141 ENT","district":null,"block":null,"subdivisionName":null,"township":null},"id":"112166032-2010","state":"OH","lotSizeDepth":null,"apn":"R8000-017.000-071","marketImprovementValue":76140,"totalValue":36932,"address":{"zip":"45067","city":"Trenton","zip4":"1117","streetPre":null,"house":"431","unitType":null,"unit":null,"street":"Kenwood","houseExt":null,"streetSuffix":"Ln","state":"OH","streetPost":null,"full":" 431 Kenwood Ln Trenton OH 45067"},"landUseCode":"RR101","landUseGeneral":"RR","coordinates":[-84.465074,39.489885],"zpid":"33237764","fips":"39017","landValue":10283,"zoningCode":null,"lotSizeAcres":0.27,"garages":[],"improvementValue":26649,"taxAmount":1159.18,"lotSizeSquareFeet":11761.2,"url":"api.bridgedataoutput.com/api/v2/pub/assessments/112166032-2010"},{"landUseDescription":"Single Family Residential","zoningDescription":null,"year":2007,"marketLandValue":24950,"numberOfBuildings":1,"lotSizeIrregular":"85 X 138","county":"Butler County","pools":[],"censusTract":"390170118002005","areas":[{"areaSquareFeet":1492,"type":"Living Building Area"},{"areaSquareFeet":1144,"type":"Base Building Area"},{"areaSquareFeet":1140,"type":"Adjusted Building Area"},{"areaSquareFeet":1492,"type":"Zillow Calculated Finished Area"},{"areaSquareFeet":348,"type":"Addition Area"}],"building":[{"airConditioning":"Central","foundation":null,"architecturalStyle":"Ranch/Rambler","yearBuilt":1965,"fireplace":null,"noOfUnits":1,"effectiveYearBuilt":null,"sewer":"Municipal/Public","heating":"Central","class":null,"comments":null,"quarterBaths":null,"totalStories":1,"kitchens":2,"water":"Municipal/Public","classDescription":null,"quality":"C-","totalRooms":5,"bedrooms":3,"condition":"Average","baths":null,"threeQuarterBaths":null,"halfBaths":null,"occupancyStatus":null,"fullBaths":1}],"lotSizeFrontage":null,"parcelID":"112166032","marketValueYear":2007,"lotTopography":"Rocky","ownerName":["ROBERT D SUTTON"],"taxYear":2007,"marketTotalValue":107700,"taxID":"61840","taxExemption":null,"legal":{"phase":null,"tract":null,"city":null,"municipality":null,"section":null,"lot":null,"otherLot":null,"unit":null,"lotDescription":"1141 ENT","district":null,"block":null,"subdivisionName":null,"township":null},"id":"112166032-2007","state":"OH","lotSizeDepth":null,"apn":"R8000-017.000-071","marketImprovementValue":82750,"totalValue":37696,"address":{"zip":"45067","city":"Trenton","zip4":"1117","streetPre":null,"house":"431","unitType":null,"unit":null,"street":"Kenwood","houseExt":null,"streetSuffix":"Ln","state":"OH","streetPost":null,"full":" 431 Kenwood Ln Trenton OH 45067"},"landUseCode":"RR101","landUseGeneral":"RR","coordinates":[-84.465074,39.489885],"zpid":"33237764","fips":"39017","landValue":8733,"zoningCode":null,"lotSizeAcres":0.27,"garages":[],"improvementValue":28963,"taxAmount":1258.38,"lotSizeSquareFeet":11761.2,"url":"api.bridgedataoutput.com/api/v2/pub/assessments/112166032-2007"},{"landUseDescription":"Single Family Residential","zoningDescription":null,"year":2014,"marketLandValue":26930,"numberOfBuildings":1,"lotSizeIrregular":"85 X 138","county":"Butler County","pools":[],"censusTract":"390170118002005","areas":[{"areaSquareFeet":1492,"type":"Living Building Area"},{"areaSquareFeet":1144,"type":"Base Building Area"},{"areaSquareFeet":1140,"type":"Adjusted Building Area"},{"areaSquareFeet":1492,"type":"Zillow Calculated Finished Area"},{"areaSquareFeet":348,"type":"Addition Area"}],"building":[{"airConditioning":"Central","foundation":null,"architecturalStyle":"Ranch/Rambler","yearBuilt":1965,"fireplace":null,"noOfUnits":1,"effectiveYearBuilt":null,"sewer":"Municipal/Public","heating":"Central","class":null,"comments":null,"quarterBaths":null,"totalStories":1,"kitchens":2,"water":"Municipal/Public","classDescription":null,"quality":"C-","totalRooms":5,"bedrooms":3,"condition":"Average","baths":null,"threeQuarterBaths":null,"halfBaths":null,"occupancyStatus":null,"fullBaths":1}],"lotSizeFrontage":null,"parcelID":"112166032","marketValueYear":2014,"lotTopography":"Rocky","ownerName":["ROBERT D SUTTON"],"taxYear":2013,"marketTotalValue":89400,"taxID":"121009","taxExemption":null,"legal":{"phase":null,"tract":null,"city":null,"municipality":null,"section":null,"lot":null,"otherLot":null,"unit":null,"lotDescription":"1141 ENT","district":null,"block":null,"subdivisionName":null,"township":null},"id":"112166032-2014","state":"OH","lotSizeDepth":null,"apn":"R8000-017.000-071","marketImprovementValue":62470,"totalValue":31290,"address":{"zip":"45067","city":"Trenton","zip4":"1117","streetPre":null,"house":"431","unitType":null,"unit":null,"street":"Kenwood","houseExt":null,"streetSuffix":"Ln","state":"OH","streetPost":null,"full":" 431 Kenwood Ln Trenton OH 45067"},"landUseCode":"RR101","landUseGeneral":"RR","coordinates":[-84.465074,39.489885],"zpid":"33237764","fips":"39017","landValue":9430,"zoningCode":null,"lotSizeAcres":0.27,"garages":[],"improvementValue":21860,"taxAmount":1678.56,"lotSizeSquareFeet":11761.2,"url":"api.bridgedataoutput.com/api/v2/pub/assessments/112166032-2014"},{"landUseDescription":"Single Family Residential","zoningDescription":null,"year":2003,"marketLandValue":23760,"numberOfBuildings":1,"lotSizeIrregular":"85 X 138","county":"Butler County","pools":[],"censusTract":"390170118002005","areas":[{"areaSquareFeet":1492,"type":"Living Building Area"},{"areaSquareFeet":1144,"type":"Base Building Area"},{"areaSquareFeet":1140,"type":"Adjusted Building Area"},{"areaSquareFeet":1492,"type":"Zillow Calculated Finished Area"},{"areaSquareFeet":348,"type":"Addition Area"}],"building":[{"airConditioning":"Central","foundation":null,"architecturalStyle":"Ranch/Rambler","yearBuilt":1965,"fireplace":null,"noOfUnits":1,"effectiveYearBuilt":null,"sewer":"Municipal/Public","heating":"Central","class":null,"comments":null,"quarterBaths":null,"totalStories":1,"kitchens":2,"water":"Municipal/Public","classDescription":null,"quality":"C-","totalRooms":5,"bedrooms":3,"condition":"Average","baths":null,"threeQuarterBaths":null,"halfBaths":null,"occupancyStatus":null,"fullBaths":1}],"lotSizeFrontage":null,"parcelID":"112166032","marketValueYear":2003,"lotTopography":"Rocky","ownerName":["ROBERT D SUTTON"],"taxYear":2003,"marketTotalValue":102590,"taxID":null,"taxExemption":null,"legal":{"phase":null,"tract":null,"city":null,"municipality":null,"section":null,"lot":null,"otherLot":null,"unit":null,"lotDescription":"1141 ENT","district":null,"block":null,"subdivisionName":null,"township":null},"id":"112166032-2003","state":"OH","lotSizeDepth":null,"apn":"R8000-017.000-071","marketImprovementValue":78830,"totalValue":35907,"address":{"zip":"45067","city":"Trenton","zip4":"1117","streetPre":null,"house":"431","unitType":null,"unit":null,"street":"Kenwood","houseExt":null,"streetSuffix":"Ln","state":"OH","streetPost":null,"full":" 431 Kenwood Ln Trenton OH 45067"},"landUseCode":"RR101","landUseGeneral":"RR","coordinates":[-84.465074,39.489885],"zpid":"33237764","fips":"39017","landValue":8316,"zoningCode":null,"lotSizeAcres":0.27,"garages":[],"improvementValue":27591,"taxAmount":1380.02,"lotSizeSquareFeet":11761.2,"url":"api.bridgedataoutput.com/api/v2/pub/assessments/112166032-2003"},{"landUseDescription":"Single Family Residential","zoningDescription":null,"year":2009,"marketLandValue":29380,"numberOfBuildings":1,"lotSizeIrregular":"85 X 138","county":"Butler County","pools":[],"censusTract":"390170118002005","areas":[{"areaSquareFeet":1492,"type":"Living Building Area"},{"areaSquareFeet":1144,"type":"Base Building Area"},{"areaSquareFeet":1140,"type":"Adjusted Building Area"},{"areaSquareFeet":1492,"type":"Zillow Calculated Finished Area"},{"areaSquareFeet":348,"type":"Addition Area"}],"building":[{"airConditioning":"Central","foundation":null,"architecturalStyle":"Ranch/Rambler","yearBuilt":1965,"fireplace":null,"noOfUnits":1,"effectiveYearBuilt":null,"sewer":"Municipal/Public","heating":"Central","class":null,"comments":null,"quarterBaths":null,"totalStories":1,"kitchens":2,"water":"Municipal/Public","classDescription":null,"quality":"C-","totalRooms":5,"bedrooms":3,"condition":"Average","baths":null,"threeQuarterBaths":null,"halfBaths":null,"occupancyStatus":null,"fullBaths":1}],"lotSizeFrontage":null,"parcelID":"112166032","marketValueYear":2009,"lotTopography":"Rocky","ownerName":["ROBERT D SUTTON"],"taxYear":2009,"marketTotalValue":105520,"taxID":"478717","taxExemption":null,"legal":{"phase":null,"tract":null,"city":null,"municipality":null,"section":null,"lot":null,"otherLot":null,"unit":null,"lotDescription":"1141 ENT","district":null,"block":null,"subdivisionName":null,"township":null},"id":"112166032-2009","state":"OH","lotSizeDepth":null,"apn":"R8000-017.000-071","marketImprovementValue":76140,"totalValue":36932,"address":{"zip":"45067","city":"Trenton","zip4":"1117","streetPre":null,"house":"431","unitType":null,"unit":null,"street":"Kenwood","houseExt":null,"streetSuffix":"Ln","state":"OH","streetPost":null,"full":" 431 Kenwood Ln Trenton OH 45067"},"landUseCode":"RR101","landUseGeneral":"RR","coordinates":[-84.465074,39.489885],"zpid":"33237764","fips":"39017","landValue":10283,"zoningCode":null,"lotSizeAcres":0.27,"garages":[],"improvementValue":26649,"taxAmount":1154,"lotSizeSquareFeet":11761.2,"url":"api.bridgedataoutput.com/api/v2/pub/assessments/112166032-2009"}],"total":21}
将 JSON 解码为 php 是一个复杂的操作:它是 Javascript 马和 php 驴的杂交。所以我认为它可能会有点混乱,如果不是固执的话。
这里有两种方法可以将埋在 JSON 对象中的平方英尺相加。
/* php objects */
$ResponseInfo = json_decode( $json, false );
$sqft = 0;
foreach ( $ResponseInfo->bundle as $bundle ) {
foreach ( $bundle->areas as $area ) {
$sqft += $area->areaSquareFeet;
}
}
/* php traditional associative arrays */
$ResponseInfo = json_decode( $json, true );
$sqft = 0;
foreach ( $ResponseInfo['bundle'] as $bundle ) {
foreach ( $bundle['areas'] as $area ) {
$sqft += $area['areaSquareFeet'];
}
}
在任何一种情况下,当您从 json_decode
的 $ResponseInfo
输出开始时,您必须明确地从该对象的顶层导航到您需要的数据。这种表达方式不行。
$sqft = $ResponseInfo->building[0]->squarefeet;
building
不是该解码对象的成员,并且在您的 JSON.
squarefeet
的成员
编辑
此表达式访问 JSON 中第一个 bundle 对象中的第一个建筑对象(当解码为 php 对象时)。
$ResponseInfo->bundle[0]->building[0]->airConditioning;
如您所知,API 响应的有效负载位于名为 bundle
的数组中。 (top-level 响应中的其他内容是显示请求状态的 API 包装器。)所以上面的表达式查看 bundle 数组的第一个元素以找到构建数组。然后它查看建筑数组的第一个元素以找到名为 noOfUnits
.
我应该解释一下我做了什么——我是如何得出答案的。我有启用了 JSON 格式化程序插件的记事本++。我将您的 JSON 放入该编辑器的草稿文档中并对其进行了格式化。然后逐层探索您的 JSON 对象变得容易。
php 对象有一个不幸的 属性 引用 $object->undefined
returns null 而不是抛出错误。空值上的 foreach()
只循环零次。
这个 post 是一个狗屎节目,所以很抱歉,如果我只是浏览了一下,请使用代码块,这样我们就可以真正解决问题,而不是在里面乱扔一堆数据摆在我们面前的,很可能对手头的问题毫无用处。
您好像在使用 stdClass Object
,这可能会让您失望(目前只是猜测)。
如果您想将数据完全转换为 stdClass 对象或完全转换为数组以便于管理;您可以使用以下任一功能:
function array_to_stdClass(&$array){
$tmp = $array;
$array = new stdClass();
foreach($tmp as $k => $v) $array->$k = (!is_object($v) && !is_array($v)) ? $v:array_to_stdClass($v);
return $array;
}
function stdClass_to_array(&$stdClass){
$tmp = $stdClass;
$stdClass = array();
foreach($tmp as $k => $v) $stdClass[$k] = (!is_object($v) && !is_array($v)) ? $v:stdClass_to_array($v);
return $stdClass;
}
在这里直播 Demo/Example:https://onlinephp.io/c/2d00a
嗯。我是个白痴。感谢所有看过的人。对不起,我浪费了你的时间。
访问我需要的数据的正确方法实际上是:
$ResponseInfo->bundle[0]->building[0]->airConditioning;
当然调整为正确的变量和成员。我遇到问题的原因是我有狭隘的视野,当我试图将我的代码减少到最简单的元素时,我没有完全做到。
特别是,我试图调试的代码在循环中被反复调用以提取 属性 信息,当我简化事情时我忘记了这一点。我的测试是提取五个属性的数据,我专注于第一个,它的数据是正确的,并且经过我和其他人的反复验证。
然而,循环中的第二个 属性 并没有发挥得那么好。事实证明,为 属性 返回的 JSON 数据实际上缺少我需要的元素。那当然是完全不同的问题了。
仅仅因为抛出了一个错误,并不一定意味着它是循环的第一次迭代导致的,而我忽略了这一点。尽管我正在处理的代码是如此模块化,但我的克星循环已从我关注的例程中完全删除。
我可能应该刚刚删除了这个问题,但我认为那些花时间查看它的人会很感激跟进,也许它会帮助其他人在进行故障排除时考虑更大的前景。
再次感谢大家。 史蒂夫