PHP json_decode在解析JSON时显示null

时间:2016-04-20 19:59:41

标签: php json parsing

我正在尝试解析JSON数据,但在json_decode() var_dump()时,null显示的值为<?php $json='_variable_1461092903017=[ { message:"success", data1:{ datalist:[ {field1:"value1",field2:"value2"} , {field1:"value1",field2:"value2"} , {field1:"value1",field2:"value2"} ] }, data2:[ { Date:"20 Apr 2016", details:[ {Code:"123",name:"xyz"}, {Code:"456",name:"abc"}, ], }, { Date:"21 Apr 2016", details:[ {Code:"123",name:"xyz"}, {Code:"456",name:"abc"}, ], }, { Date:"22 Apr 2016", details:[ {Code:"123",name:"xyz"}, {Code:"456",name:"abc"}, ], } ]} ]'; $json_data = json_decode($json); var_dump($json_data); ?> 。以下是我的计划:

.readLine

1 个答案:

答案 0 :(得分:-2)

像其他人说的那样是无效的JSON
您可以使用JSON Linter http://jsonlint.com

进行调试

酷有效的JSON铁路图:
http://www.json.org

这应该是你要找的东西(我没有执行它):

<?php

$json='[{
    "message" :"success",
    "data1":{
        "datalist" :[
                    { "field1":"value1","field2":"value2"},
                    {"field1":"value1","field2":"value2"},
                    {"field1":"value1","field2":"value2"}
        ]
    },
    "data2":[ {
        "Date":"20 Apr 2016",
        "details":[
                    {"Code":"123","name":"xyz"},
                    {"Code":"456","name":"abc"}
                ]
        }, 
        {
        "Date":"21 Apr 2016",
        "details":[
                    {"Code":"123","name":"xyz"},
                    {"Code":"456","name":"abc"}
                ]
        }, 
        {
        "Date":"22 Apr 2016",
        "details":[
                    {"Code":"123","name":"xyz"},
                    {"Code":"456","name":"abc"}
                ]
        }
    ]}
]';

$json_data = json_decode($json);
var_dump($json_data);

JSON中的错误:

您需要双重引用密钥:

{
    "key": "value"
}

{
    key: "value"
}

关闭数组括号]后,您要添加,

应该是这样的:

{
    "datalist": [ "blah", "blah"]
}

{
    "datalist": [ "blah", "blah"],
}

数组中的最后一个元素后面不应该有逗号:

{
   "datalist": [{"key1":"value1", {"key2": "value2"}]
}

{
   "datalist": [{"key1":"value1", {"key2": "value2"},]
}