php json_decode失败,键上没有引号 [英] php json_decode fails without quotes on key

查看:344
本文介绍了php json_decode失败,键上没有引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样表示的json数据

I have json data represented like this

{key:"value"}

(无引号引起来的键...)

(no quotes arround key...)

我想将其转换为关联数组.

I want to translate it to an associative array.

PHP的 json_decode 返回null

PHP's json_decode returns null

如何在密钥周围添加引号? 谢谢...

How can I add the quotes around the key?? thanks...

推荐答案

您可以在源代码处修复JSON,以使其返回有效的JSON结构,也可以在关键字周围手动添加引号.

You can either fix the JSON at the source so that it returns a valid JSON structure, or you can manually add quotes around the keys.

类似问题的答案中有一个示例为此:

function my_json_decode($s) {
    $s = str_replace(
        array('"',  "'"),
        array('\"', '"'),
        $s
    );
    $s = preg_replace('/(\w+):/i', '"\1":', $s);
    return json_decode(sprintf('{%s}', $s));
}

这篇关于php json_decode失败,键上没有引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆