如何使用php读取json文件

时间:2016-04-11 13:21:49

标签: php json

我的JSON文件是这样的,我想用PHP读取这个文件:

{
    "data": [
        {
            "time": 1383458400,
            "summary": "Mostly cloudy throughout the day.",        
        },
        {
            "time": 1383458400,
            "summary": "Mostly cloudy throughout the day.",        
        }
    ],
    "data": [
        {
            "time": 1383458400,
            "summary": "Mostly cloudy throughout the day.",        
        },
        {
            "time": 1383458400,
            "summary": "Mostly cloudy throughout the day.",        
        }
    ]
}

如何在PHP中读取此文件?我使用以下内容阅读:

$str = file_get_contents('files/filenameme.json');
$json = json_decode($str, true); 

1 个答案:

答案 0 :(得分:1)

你自己写过JSON吗?

你有两个问题:“数据”是重复的,这应该是唯一的,如“data1”,“data2”。另一个问题是摘要部分后面的逗号 - 它不属于那里。

如果你解决了这些问题,你应该好好去。