【问题标题】:Php - json_decode returns null [closed]Php - json_decode 返回 null [关闭]
【发布时间】:2018-05-27 07:35:35
【问题描述】:

我正在尝试使用 json_decode php 函数解码如下所示的 json 字符串:

"{id:"1",fields:[{id:"1",value:"asdasd"},{id: "2",value:"asdasd"},{id:"3",value:"asdasd"}]}"

我已经尝试了来自 question 的各种选项。喜欢:

$foo = utf8_encode($json_string);
$data = json_decode($foo, true);

或者:

json_decode(str_replace ('\"','"', $json_string), true);

即使:

json_decode( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $json_string), true );

但是,我尝试的每件事都会得到null。 这是为什么呢?

【问题讨论】:

  • 如果你只是 json_decode 会发生什么?
  • 同样的事情,我也得到了 null
  • 它的json无效,不要尝试自己构建json使用json_encode()
  • "json_decode 返回 null" 对,这就是它对无效 JSON 的作用,例如您的示例。在有效的 JSON 中,属性名称位于 " 中。投票结束为错字/不可复制/未来对其他人无用。
  • 你应该有像{"id":"1"这样的json而不是{id:"1"

标签: php json


【解决方案1】:

首先,您的 JSON 无效。在询问任何JSON lint tool 之前,您应该始终首先验证您的 JSON,这会告诉您错误的确切位置和错误。

此外,在 PHP 中处理 JSON 时,您应该始终检查错误。您可以查看json_last_error 函数的官方文档,了解如何正确执行验证。

$json = 'your_json_string';
$array = json_decode($json, true);

if (json_last_error()) {
    die('Invalid JSON provided!');
}

printf('<pre>Valid JSON output: %s</pre>', print_r($array, true));

值得一提:由于添加了PHP 7.3 JSON_THROW_ON_ERROR 选项,因此可以使用 try-catch 块包裹代码:

try {
    $array = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
    printf('Valid JSON output: %s', print_r($array, true));
} catch (\JsonException $exception) {
    printf('Invalid JSON input: %s', print_r($exception, true));
}

Working example.

【讨论】:

  • 我建议你使用json_decode($json,true)来获取一个数组而不是std类对象。
【解决方案2】:

您的 JSON 返回 Null,因为在 JSON 中,键应该是字符串。您的 JSON 格式不正确,因此返回 Null。 尝试将其重写为:

 {
  "id":"1",
  "fields":[
    {
      "id":"1",
      "value":"asdasd"

    },
    {
      "id": "2",
      "value":"asdasd"

    },{
      "id":"3",
      "value":"asdasd"
      }
    ]
}

【讨论】:

    【解决方案3】:
    <?php
    $data='{
        "id": "1",
        "fields": [{
            "id": "1",
            "value": "asdasd"
        }, {
            "id": "2",
            "value": "asdasd"
        }, {
            "id": "3",
            "value": "asdasd"
        }]
    }';
    
    $dataNew=json_decode($data,true);
    echo '<pre>';
        print_r($dataNew);
    

    您的 json 无效。 json-keys 需要在双引号内。之后 json_decode 就可以正常工作了。

    输出是:

    Array
    (
        [id] => 1
        [fields] => Array
            (
                [0] => Array
                    (
                        [id] => 1
                        [value] => asdasd
                    )
    
                [1] => Array
                    (
                        [id] => 2
                        [value] => asdasd
                    )
    
                [2] => Array
                    (
                        [id] => 3
                        [value] => asdasd
                    )
    
            )
    
    )
    

    【讨论】:

      猜你喜欢
      • 2012-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-13
      • 1970-01-01
      • 2019-04-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多