php json_decode fails without quotes on key

后端 未结 8 1911
故里飘歌
故里飘歌 2020-12-06 06:26

I have json data represented like this

{key:\"value\"}

(no quotes arround key...)

I want to translate it to an associative array.

8条回答
  •  [愿得一人]
    2020-12-06 06:50

    If you can't turn that into valid JSON at the source, then you can use Services_JSON from PEAR to parse it, since adding quotes around the key is a non-trivial error-prone process.

    Services_JSON will correctly parse the invalid key string.

    Example:

    $json = new Services_JSON();
    var_dump($json->decode('{key:"value"}'));
    

    Output:

    object(stdClass)#2 (1) {
      ["key"]=>
      string(5) "value"
    }
    

提交回复
热议问题