简体   繁体   中英

PHP json_decode showing null while parsing JSON

I am trying to parse JSON data but while json_decode() the var_dump() shows the value as null . Below is my program:

<?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);

?>

Like others said that is not valid JSON
you can debug it using a JSON Linter http://jsonlint.com

A cool valid JSON railroad diagram:
http://www.json.org

This should be what you are looking for (I didn't execute it):

<?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);

Errors in JSON:

You need to double quote your keys:

{
    "key": "value"
}

Not:

{
    key: "value"
}

After your closing array bracket ] you are adding a ,

should be this:

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

Not:

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

The last element in your array should not have a comma after it:

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

Not:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM