从JSON解码地址数组 [英] Adressing PHP array decoded from JSON
本文介绍了从JSON解码地址数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在加载JSON数组并将其解码为PHP数组
I'm loading a JSON array and decode it to an PHP array
$jsonfile = file_get_contents('https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=15min&outputsize=full&apikey=demo');
$jsonarray = json_decode($jsonfile);
var_dump($jsonarray);
到目前为止,我得到的数组看起来像这样:
So far so good I get an array that looks like that:
object(stdClass)#1 (2) {
["Meta Data"]=> object(stdClass)#2 (5) {
["1. Information"]=> string(49) "Daily Prices (open, high, low, close) and Volumes"
["2. Symbol"]=> string(5) "AAWVX"
["3. Last Refreshed"]=> string(10) "2017-06-30"
["4. Output Size"]=> string(9) "Full size"
["5. Time Zone"]=> string(10) "US/Eastern"
}
["Time Series (Daily)"]=> object(stdClass)#3 (105) {
["2017-06-30"]=> object(stdClass)#4 (5) {
["1. open"]=> string(7) "10.5100"
["2. high"]=> string(7) "10.5100"
["3. low"]=> string(7) "10.5100"
["4. close"]=> string(7) "10.5100"
["5. volume"]=> string(1) "0"
}
["2017-06-29"]=> object(stdClass)#5 (5) { ["1. open"]=> string(7) "10.4800" ["2. high"]=> string(7) "10.4800" ["3. low"]=> string(7) "10.4800" ["4. close"]=> string(7) "10.4800" ["5. volume"]=> string(1) "0" }
["2017-06-28"]=> object(stdClass)#6 (5) { ["1. open"]=> string(7) "10.5600" ["2. high"]=> string(7) "10.5600" ["3. low"]=> string(7) "10.5600" ["4. close"]=> string(7) "10.5600" ["5. volume"]=> string(1) "0" } ...
但是当我尝试像这样处理数组时
But when I try to adress the array like
var_dump($jsonarray['Meta Data']);
它不起作用.
推荐答案
这是因为没有参数的json_decode()
试图将json字符串转换为stdClass对象.如果要将其转换为数组,则需要将第二个参数($assoc
布尔值)设置为true
:
This is because json_decode()
with no parameters attempts to convert your json string to an stdClass object. If you want to convert it to an array, you need to set the 2nd parameters (the $assoc
boolean) to true
:
$json = file_get_contents('LINK TO JSON OUTPUT');
$array = json_decode($json, true);
这篇关于从JSON解码地址数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文