I am having trouble accessing information in a json-Array that I retrieved using php's json_decode
function.
我在使用php的json_decode函数检索的json数组中访问信息时遇到了麻烦。
the json-file looks as follows:
json文件如下:
{"code":0,"message":"Okay","model":{"results":[{"message":"Okay","balance":0,"openPositions":[[],[]],"firstDepositDate":XXX,"currencySign":"€","email":"X.X@X.com","code":0}]},"result":"success"}
I used the following php code to get the contents:
我使用以下php代码获取内容:
$json = file_get_contents($json_url);
$data = json_decode($json,true);
echo '<pre>' . print_r($json, true) . '</pre>';
The result print_r displays looks just like what I expect and looks like the json.
结果print_r显示的结果与我预期的一样,看起来像json。
However, I cannot figure out to access the variables. Whenever I try something like
但是,我无法找到访问变量的方法。每当我尝试类似的东西
$test = $json['model']['results']['balance'];
the script throws an error, which I can't identify. I already figured out if I access the $json
variable like so:
脚本抛出一个错误,我无法识别。我已经知道如果我访问$json变量,比如:
$test = $json[n]; // returns the nth character, e.g. n = 0 $test = "{", n = 2 $test = "c"
The script also did not throw an error if I tried accessing the variable like so:
如果我尝试像这样访问变量,脚本也不会抛出错误:
$test = $json['code']; // returns "{"
Can someone help me figure out how to navigate this array?
有人能帮我找到如何导航这个数组吗?
Thanks!
谢谢!
3 个解决方案
#1
4
The results
key is a true array, not an unordered map (key/value). Additionally, you should be accessing the decoded $data
, not the $json
string.
结果键是一个真实的数组,而不是无序映射(键/值)。此外,您应该访问解码后的$数据,而不是$json字符串。
This should work for you:
这应该对你有用:
$test = $data['model']['results'][0]['balance'];
This is what should have tipped you off:
这就是应该告诉你的:
{"results":[{"message"
^ ^
| |
| \-- Start of an array
|
\-- Start of an object
#2
1
You recieve an error similar to Cannot use object of type stdClass as array in (...)
right?
您收到一个类似于无法在(…)中使用类型为stdClass的对象作为数组的错误,对吗?
json_decode returns an object of the type stdClass
which is the only object in php (as far as I know) which properties cannot be accessed like an array. You will have to access them as follows:
json_decode返回一个stdClass类型的对象,stdClass是php中惟一的对象(据我所知),它的属性不能像数组那样被访问。您将必须访问它们如下:
$json = '{"code":0,"message":"Okay","model":{"results":[{"message":"Okay","balance":0,"openPositions":[[],[]],"firstDepositDate":"XX","currencySign":"€","email":"X.X@X.com","code":0}]},"result":"success"}';
$obj = json_decode($json);
var_dump($obj->message); //works
var_dump($obj["message"]); //throws exception
#3
0
There is an error in the JSON array. "firstDepositDate": XXX is not a valid value. Should be a string "XXX".
JSON数组中有一个错误。“firstDepositDate”:XXX不是一个有效值。应该是字符串“XXX”。
Also you are trying to the wrong variable. The decoded data should be a PHP array. In this case $data['code'] instead of $json['code']
你也在尝试错误的变量。解码后的数据应该是一个PHP数组。在这种情况下,$data['code']而不是$json['code']
#1
4
The results
key is a true array, not an unordered map (key/value). Additionally, you should be accessing the decoded $data
, not the $json
string.
结果键是一个真实的数组,而不是无序映射(键/值)。此外,您应该访问解码后的$数据,而不是$json字符串。
This should work for you:
这应该对你有用:
$test = $data['model']['results'][0]['balance'];
This is what should have tipped you off:
这就是应该告诉你的:
{"results":[{"message"
^ ^
| |
| \-- Start of an array
|
\-- Start of an object
#2
1
You recieve an error similar to Cannot use object of type stdClass as array in (...)
right?
您收到一个类似于无法在(…)中使用类型为stdClass的对象作为数组的错误,对吗?
json_decode returns an object of the type stdClass
which is the only object in php (as far as I know) which properties cannot be accessed like an array. You will have to access them as follows:
json_decode返回一个stdClass类型的对象,stdClass是php中惟一的对象(据我所知),它的属性不能像数组那样被访问。您将必须访问它们如下:
$json = '{"code":0,"message":"Okay","model":{"results":[{"message":"Okay","balance":0,"openPositions":[[],[]],"firstDepositDate":"XX","currencySign":"€","email":"X.X@X.com","code":0}]},"result":"success"}';
$obj = json_decode($json);
var_dump($obj->message); //works
var_dump($obj["message"]); //throws exception
#3
0
There is an error in the JSON array. "firstDepositDate": XXX is not a valid value. Should be a string "XXX".
JSON数组中有一个错误。“firstDepositDate”:XXX不是一个有效值。应该是字符串“XXX”。
Also you are trying to the wrong variable. The decoded data should be a PHP array. In this case $data['code'] instead of $json['code']
你也在尝试错误的变量。解码后的数据应该是一个PHP数组。在这种情况下,$data['code']而不是$json['code']