php json_decode fails without quotes on key

后端 未结 8 1910
故里飘歌
故里飘歌 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:56

    DON'T USE REGEX.

    REGEX is totally not recommended for such cases, don't use that for parsing such data. If you have a simple goals and want to avoid the stable PEAR package, then you might try JSON-php library (but no-longer maintained).

    1) Get JSON.phps file from here and rename to .php and replace constructor function names to __construct.

    2) usage:

    $content = '{myKey:"valueeeee"}';
    
    include(__DIR__.'/JSON.php'); 
    $this->json = new Services_JSON( SERVICES_JSON_LOOSE_TYPE );  // to return objects instead of Associative array, remove the argument
    var_dump( $this->json->decode($content)  );
    

提交回复
热议问题