在PHP中从JSON获取数据
I am trying to get the data out of this JSON format to be able to show on screen problem is I am not sure how I would be able to do this as I have tried multiple ways such as :
我试图从这个JSON格式获取数据,以便能够在屏幕上显示问题是我不知道我将如何做到这一点,因为我尝试了多种方式,如:
$stats = json_decode($result);
// var_dump($stats);
echo $stats->elo;
And nothing is working I am unsure how to get the data due to the [], as I have never worked with this before. As shown below is a small piece of the data I need to be able to get into.
没有什么工作我不确定如何获得由于[]的数据,因为我以前从未使用过这个。如下所示,我需要能够进入的一小部分数据。
[{"_id":{"championId":51,"role":"DUO_CARRY"},"elo":"BRONZE","patch":"7.11","championId":51,"positions":{"deaths":3,"winRates":6,"minionsKilled":2,"previousOverallPerformanceScore":6}}]
Thanks in advance
提前致谢
5 个解决方案
#1
1
There is a simple rule of thumb when processing JSON. First just decode it and print it using a print_r()
so you can see its structure easily
处理JSON时有一个简单的经验法则。首先解码它并使用print_r()打印它,这样您就可以轻松地看到它的结构
$s = '[{"_id":{"championId":51,"role":"DUO_CARRY"},"elo":"BRONZE","patch":"7.11","championId":51,"positions":{"deaths":3,"winRates":6,"minionsKilled":2,"previousOverallPerformanceScore":6}}]';
$stats = json_decode($s);
print_r($stats);
Which will show you in this case
这将在这种情况下向您展示
Array
(
[0] => stdClass Object
(
[_id] => stdClass Object
(
[championId] => 51
[role] => DUO_CARRY
)
[elo] => BRONZE
[patch] => 7.11
[championId] => 51
[positions] => stdClass Object
(
[deaths] => 3
[winRates] => 6
[minionsKilled] => 2
[previousOverallPerformanceScore] => 6
)
)
)
So now you know there is an array containing in this case only one object
所以现在你知道有一个数组在这种情况下只包含一个对象
So to show elo
you could do a simple
所以要展示你好,你可以做一个简单的事情
echo $stats[0]->elo; // BRONZE
But as it is an array of object it may be better to assume in some cases there will be more than one stat so you could process it in a foreach loop like this
但由于它是一个对象数组,最好假设在某些情况下会有多个统计信息,因此您可以在这样的foreach循环中处理它
foreach ($stats as $stat) {
echo $stat->elo;
}
更多相关文章
- php如何判断数组["a","b","c"]中是否含有"a"?
- php curl模拟登陆抓取数据
- 自动同步vps服务器或本地文件的数据到百度网盘
- 如果key在变量中,PHP如何从数组中获取值
- 这是什么原因啊,没有找出错来啊(数据库用的是mysql)
- 是否可以在PHP中合并这些数组? [重复]
- 将数据从一个流传输到另一个流
- PHP打印输出数组内容及结构函数print_r与var_dump
- 使用ORM在不在数据库中的表上的外键