使用PHP从JSON文件获取数据(带数字的空结果)

时间:2017-11-30 23:57:53

标签: php json

我试图使用PHP

从以下JSON文件中获取数据

我想获得所有URls(链接),

http://www.google.com 
http://www.bing.com 

的Json

enter image description here

这是我试过的

PHP

enter image description here

由于

1 个答案:

答案 0 :(得分:1)

您无法使用数字访问对象属性,它必须是字符串。

json_decode作为数组输出并以这种方式访问​​属性要容易得多。为此,请将true作为第二个参数。

<?php

$json = '
{
    "news": {
        "name": "yahoo",
        "url": "https://yahoo.com"
    },
    "links": [
        {
            "id": "1",
            "url": "https://google.com"
        },
        {
            "id": "2",
            "url": "https://bing.com"
        }
    ]
}';

$decode = json_decode($json, true);

echo $decode['links'][0]['url'];
相关问题