嵌套数组的json_decode返回一个空值

时间:2015-04-27 07:11:19

标签: php arrays json

我有一个嵌套数组,我使用json_encode()转换为JOSN,当我尝试使用json_decode()将其转换回数组表单时,返回的结果为空。

$arr = json_decode($json,true);
下面的

是json变量的print_r的结果:

[{
    "result":"SUCCESS",
    "msg":{
        "type":"localScan",
        "res":{
            "status":"0",
            "scan":[
                {
                    "name":"AVG",
                    "result":"0",
                    "type":"Clean"
                },{
                    "name":"ESET NOD32",
                    "result":"2",
                    "type":"Not Working"
                },{
                    "name":"AVAST",
                    "result":0,
                    "type":"Clear"
                },{
                    "name":"Kaspersky",
                    "result":"2",
                    "type":"Not Working"
                },{
                    "name":"Bit-defender",
                    "result":"2",
                    "type":"Not Working"
                }
            ]
        }
    }
},{
    "result":"SUCCESS",
    "msg":{
        "type":"localScan",
        "res":{
            "status":"1",
            "scan":[{
                    "name":"AVG",
                    "result":"1",
                    "type":"Autorun"
                },
                {
                    "name":"ESET NOD32",
                    "result":"2",
                    "type":"Not Working"
                },
                {
                    "name":"AVAST",
                    "result":2,
                    "type":"Not Working"
                },
                {
                    "name":"Kaspersky",
                    "result":"2",
                    "type":"Not Working"
                },
                {
                    "name":"Bit-defender",
                    "result":"2",
                    "type":"Not Working"
                }
            ]
        }
    }
}]

这是一个用于创建JSON的部分数组,json_decode函数返回值为FALSE。

Array ( 
    [0] => Array ( 
        [result] => SUCCESS 
        [msg] => Array ( 
            [type] => localScan 
            [res] => Array ( 
                [status] => 0 
                [scan] => Array ( 
                    [0] => Array ( 
                        [name] => AVG 
                        [result] => 0 
                        [type] => Clean 
                    ) 
                    [1] => Array ( 
                        [name] => ESET NOD32 
                        [result] => 2 
                        [type] => Not Working 
                    ) 
                    [2] => Array ( 
                        [name] => AVAST 
                        [result] => 0 
                        [type] => Clear 
                    ) 
                    [3] => Array 
                    ( 
                        [name] => Kaspersky 
                        [result] => 2 
                        [type] => Not Working 
                    ) 
                    [4] => Array ( 
                        [name] => Bit-defender 
                        [result] => 2 
                        [type] => Not Working 
                    )
                )
            ) 
        ) 
    )
)

2 个答案:

答案 0 :(得分:1)

按照您可以尝试的代码,因为我从您提出的问题中获取数据: -

$jsonString='<String of the Json>'; $data = json_decode($jsonString); echo '<pre>'; print_r($data);

跳这会帮助你

答案 1 :(得分:0)

我认为你没有回应json字符串 -

<?php
  $arr = array(); // your array
  echo json_encode( $arr );
?>