这是我的JSON解析器函数中的一小段代码,它可以很好地处理大约十几个不同的JSON文件:
这些打印输出是为了当前调试目的
$this->response = stripslashes($this->response);
print_r($this->response);
$this->response = json_decode($this->response);
print_r($this->response);
第一次打印输出了JSON字符串,而第二次打印输出了null。通过使用json_last_error
函数,我发现PHP拒绝解析JSON,因为它的语法无效。
这是我正在解析的JSON的片段(完整代码在这里),可以通过我在Google上找到的所有验证器进行验证:
{
"success": 1,
"stores": [
{
"name": "Winton",
"address": "370-374 , Wimborne Road, Bournemouth, Dorset BH92HE",
"telephone": "",
"email": "info@99pstoresltd.com",
"website": "",
"description": "Mon - 09.00-18.00 Tue - 09.00-18.00 Wed - 09.00-18.00 Thu - 09.00-18.00 Fri - 09.00-18.00 Sat - 09.00-18.00 Sun - 10.00-16.00",
"lat": "50.7413",
"lng": "-1.87926",
"titlewebsite": "Website",
"titleemail": "Email",
"titletel": "Telephone",
"titlecontactstore": "Contact this store",
"titlekm": "km",
"titlemiles": "miles",
"cat_name": "",
"cat_img": "",
"img": ""
},
{
"name": "Boscombe",
"address": "The Sovereign Centre, Boscombe, Bournemouth, Dorset BH14SX",
"telephone": "",
"email": "info@99pstoresltd.com",
"website": "",
"description": "Mon - 08.00-18.00 Tue - 08.00-18.00 Wed - 08.00-18.00 Thu - 08.00-18.00 Fri - 08.00-18.00 Sat - 08.00-18.00 Sun - 10.00-16.00",
"lat": "50.7272",
"lng": "-1.83952",
"titlewebsite": "Website",
"titleemail": "Email",
"titletel": "Telephone",
"titlecontactstore": "Contact this store",
"titlekm": "km",
"titlemiles": "miles",
"cat_name": "",
"cat_img": "",
"img": ""
}]
}
我不知道为什么这个JSON无法解析,因为在我的看来它看起来很好!我一直盯着它看了很长时间,所以非常希望能听到大家的想法。
编辑: 从第一个print_r中复制并粘贴数据,然后通过json_decode处理可以正常工作。我猜这意味着JSON的来源存在问题,可能会影响编码或其他方面?
json_decode()
仍然给我返回了null值。 - jackmullenhtmlentities($this->response);
和html_entity_decode($this->response);
,但仍然从json_decode()
得到了空值。 - jackmullen