【发布时间】: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"