非标准的json 格式示例
1 | { 'asin' : '0001048791' , 'salesRank' : { 'Books' : 6334800}, 'imUrl' : 'http://ecx.images-amazon.com/images/I/51MKP0T4DBL.jpg' , 'categories' : [[ 'Books' ]], 'title' : 'The Crucible: Performed by Stuart Pankin, Jerome Dempsey & Cast' } |
处理示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | $string = "{'asin': '0001048791', 'salesRank': {'Books': 6334800}, 'imUrl': 'http://ecx.images-amazon.com/images/I/51MKP0T4DBL.jpg', 'categories': [['Books']], 'title': 'The Crucible: Performed by Stuart Pankin, Jerome Dempsey & Cast'}" ; $fixedJSON = fixJSON( $string ); function fixJSON( $json ) { $regex = <<< 'REGEX' ~ "[^" \\]*(?:\\.|[^ "\\]*)*" (*SKIP)(*F) | '([^' \\]*(?:\\.|[^ '\\]*)*)' ~x REGEX; return preg_replace_callback( $regex , function ( $matches ) { return '"' . preg_replace( '~\\\\.(*SKIP)(*F)|"~' , '\\"' , $matches [1]) . '"' ; }, $json ); } $decoded = json_decode( $fixedJSON ); var_dump( $fixedJSON ); print_r( $decoded ); |
输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | stdClass Object ( [asin] => 0001048791 [salesRank] => stdClass Object ( [Books] => 6334800 ) [imUrl] => http: //ecx.images-amazon.com/images/I/51MKP0T4DBL.jpg [categories] => Array ( [0] => Array ( [0] => Books ) ) [title] => The Crucible: Performed by Stuart Pankin, Jerome Dempsey & Cast ) |