我正在尝试使用来自json_decode的数组中的一些值,但无法从var_dump获取任何输出(这可能会帮助我弄清楚如何访问特定值)。我也从类似问题的建议中尝试了$ data [0]。
<?PHP
$json = 'callback({"geobytesinternet":"US","geobytescountry":"United States","geobytesregionlocationcode":"USWI","geobytesregion":"Wisconsin","geobytescode":"WI","geobyteslocationcode":"USWILOLA","geobytescity":"Land O Lakes","geobytescityid":"30028","geobytesfqcn":"Land O Lakes, WI, United States","geobyteslatitude":"46.154598","geobyteslongitude":"-89.396896","geobytescapital":"Washington, DC ","geobytestimezone":"-89.3969","geobytesnationalitysingular":"American","geobytespopulation":"278058881","geobytesnationalityplural":"Americans","geobytesmapreference":"North America ","geobytescurrency":"US Dollar","geobytescurrencycode":"USD","geobytestitle":"The United States"});';
$json = str_replace( 'callback(', '', $json);
$json = str_replace( ');', '', $json);
$json = '[' . $json . ']';
$data = json_decode($json);
echo '<br>decoded: ' . $data;
echo '<br><br>var_dump: ' . var_dump($data[0]);
?>
答案 0 :(得分:0)
你几乎,函数json_decode()
作为输入 json encoded 字符串。由于此行$json = '[' . $json . ']';
。
这是正确编码的json字符串
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
NULL
。请参阅documentation。
修改强> 所以你的代码应该是这样的:
<?php
$json = 'callback({"geobytesinternet":"US","geobytescountry":"United States","geobytesregionlocationcode":"USWI","geobytesregion":"Wisconsin","geobytescode":"WI","geobyteslocationcode":"USWILOLA","geobytescity":"Land O Lakes","geobytescityid":"30028","geobytesfqcn":"Land O Lakes, WI, United States","geobyteslatitude":"46.154598","geobyteslongitude":"-89.396896","geobytescapital":"Washington, DC ","geobytestimezone":"-89.3969","geobytesnationalitysingular":"American","geobytespopulation":"278058881","geobytesnationalityplural":"Americans","geobytesmapreference":"North America ","geobytescurrency":"US Dollar","geobytescurrencycode":"USD","geobytestitle":"The United States"});';
//Compact a bit the code, note the ');' replacement
//str_replace() can take an array of arguments, so no need to call the function multiple times
$json = str_replace( array('callback(', ');'), '', $json);
//Get elements in a array
$data = json_decode($json);
//See what's inside the array
var_dump($data);
?>
This是一个工作小提琴。
答案 1 :(得分:0)
解码后的数据是对象数组,所以请执行以下操作:
$result = $data[0];
echo $result->geobytesinternet;