非标准的json 格式示例
{'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'}
处理示例
$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 = <<
~
"[^"\\]*(?:\\.|[^"\\]*)*"
(*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);
输出
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
)