php json 没有引号怎么办

来自:互联网
时间:2021-05-10
阅读:

具体问题:

没有双引号的JSON格式,有什么办法用PHP解开吗。

格式如:{name: "brandId1", type: "String1"}

json的标准格式应该是key有双引号,目前第三方数据无引号,请问有什么办法可以解开为数组吗

解决方案:

PHP代码如:

$s = <<< JSON
{name: "brandId1", type: "String1"}
JSON;
print_r(ex_json_decode($s));
function ex_json_decode($s, $mode=false) {
  if(preg_match('/\w:/', $s))
    $s = preg_replace('/(\w+):/is', '"$1":', $s);
  return json_decode($s, $mode);
}
返回顶部
顶部