获取JSON数据的特定部分

时间:2016-03-14 19:28:08

标签: php json

我想使用PHP来解析外部站点的JSON数据。此代码按预期工作:

$json = file_get_contents($url);
$obj = json_decode($json);

...和

echo $obj->title;

...从JSON数据(Far Cry Primal)发布标题:

{"id":241408,"title":"Far Cry Primal","url":"http:\/\/www.prisguide.no\/produkt\/far-cry-primal-241408","price":547,"priceCount":13,"image":{"100x100":"http:\/\/img.prisguide.no\/1678\/1678079\/original.77x100mt.jpg","300x300":"http:\/\/img.prisguide.no\/1678\/1678079\/original.232x300t.jpg","500x500":"http:\/\/img.prisguide.no\/1678\/1678079\/original.jpg"},"category":"Spill","categoryId":32,"manufacturer":"Ubisoft Montreal","specifications":"Xbox One, Action, 18+, Ubisoft","specificationsFull":[{"id":96,"name":"Plattform","type":"select","sectionName":"Generelt","value":[{"id":45275,"value":"Xbox One"}]}

但我需要访问更多元素,比如最后一个元素(向右滚动) - 值:Xbox One。这有自己的ID,但我不知道如何访问它。

任何提示?

1 个答案:

答案 0 :(得分:1)

尝试这样的事情,你的JSON不是有效的,所以我无法测试它,但我确信你明白了。

foreach($obj as $items)
{
    foreach($items as $item)
    {
        foreach($item as $value)
        {
            foreach($value as $key => $value)
            {
                echo $key . ': ' . $value;
                echo '<br>';
            }
        }
    }
}