简体   繁体   English

如何从json_decode出来的数组中获取一个值?

[英]How do I get a value from this array that came out of json_decode?

I'm trying to use a few values from this array that comes out of json_decode, but can't get any output from var_dump (which would presumably help me figure out how to access a particular value). 我正在尝试使用来自json_decode的数组中的一些值,但无法从var_dump获得任何输出(这大概可以帮助我弄清楚如何访问特定值)。 I've tried $data[0] also from a suggestion on a similar question. 我也从类似问题的建议中尝试了$ 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]);

?>

You are almost there, the function json_decode() takes as input json encoded string. 你是几乎没有,函数json_decode()作为输入JSON编码字符串。 Your string is not properly json encoded, because of this line $json = '[' . $json . ']'; 您的字符串未正确进行json编码,因为此行$json = '[' . $json . ']'; $json = '[' . $json . ']'; .

This is properly encoded json string 这是正确编码的json字符串

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit. 如果无法解码json或编码的数据深于递归限制,则返回NULL See the documentation . 请参阅文档

EDIT: So your code should look something like this: 编辑:因此您的代码应如下所示:

<?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 is a working fiddle. 是一个工作的小提琴。

The decoded data is object array so do something like this: 解码后的数据是对象数组,因此请执行以下操作:

$result = $data[0];
echo $result->geobytesinternet;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM