php生成的JSON文件错误
PHP-generarted JSON File Errors
我目前正在使用PHP脚本创建JSON文件的缓存。我们的服务器上有一个PHP文件,它可以查询一个非常大的数据库,并以JSON格式返回结果集(因此我们可以在jQuery和其他框架中使用它)。
从数据库中显示原始JSON的脚本工作得很好,我做了一个简单的缓存构建脚本,也在一定程度上工作。但是,我们注意到在生成的缓存文件中发生了一些奇怪的事情。
PHP似乎在引号中添加斜杠,以及在JSON的开始和结束处添加多余的"
。
下面是我们传入的JSON示例(注意它并不完整):
[
{
"id":1580,
"name":"Fydell House",
"address1":"South Square",
"address2":null,
"towncity":"Boston",
"county":"Lincolnshire",
"postcode":"PE21 6HU",
"addressVerbose":"South Square
Boston
Lincolnshire
PE21 6HU
",
"addressVerboseLinked":"",
"longitude":-0.022778,
"latitude":52.975806,
"londonBorough":null,
"telno":"01205 351 520",
"faxno":null,
"email":null,
"webaddress":null,
"mainimg":null,
"venueType":"Historic Building",
"description":null,
"excerpt":" ",
"images":null,
"creationDate":943920000,
"londonfeatured":false,
"unusual":false,
"featured":false,
"active":true,
"trial":false,
"modifiedDate":1234709308,
"hits":"1579",
"allowReviews":false,
"amenities":null,
"imagealt":"Lincolnshire wedding reception venue in Boston, Fydell House",
"imagetitle":"Lincolnshire wedding venues in Boston",
"car_directions":null,
"pub_directions":null,
"additional_info":null,
"listedBy":0,
"listedByName":null,
"region":null
}
]
输出JSON文件并将其存储在服务器上的PHP代码:
// Function to output the contents from the live data, and create a cache file:
function create_cache_file($url, $filename)
{
$url = str_replace(' ', '%20', $url);
$json_string = file_get_contents($url);
$file = fopen($filename, 'w');
// If there is a problem creating the file:
if(!$file)
{
die('error creating the file!');
}
else
{
fwrite($file, json_encode($json_string));
fclose($file);
echo $json_string;
}
}
文件经过PHP处理并存储在服务器上后是这样的:
"[{'"id'":437,'"name'":'"Lanteglos Country House Hotel'",'"address1'":'"Lanteglos-by-Camelford'",'"address2'":null,'"towncity'":'"Camelford'",'"county'":'"Cornwall'",'"postcode'":'"PL32 9RF'",'"addressVerbose'":'"Lanteglos-by-Camelford<br '''/>Camelford<br '''/>Cornwall<br '''/>PL32 9RF<br '''/>'",'"addressVerboseLinked'":'"'",'"longitude'":-4.695491,'"latitude'":50.612462,'"londonBorough'":null,'"telno'":'"01840 213 551'",'"faxno'":null,'"email'":null,'"webaddress'":null,'"mainimg'":null,'"venueType'":'"Hotel'",'"description'":null,'"excerpt'":'" '",'"images'":null,'"creationDate'":943920000,'"londonfeatured'":false,'"unusual'":false,'"featured'":false,'"active'":true,'"trial'":false,'"modifiedDate'":1234662248,'"hits'":'"1145'",'"allowReviews'":false,'"amenities'":null,'"imagealt'":'"Cornwall wedding reception venue in Camelford, Lanteglos Country House Hotel'",'"imagetitle'":'"Cornwall wedding venues in Camelford'",'"car_directions'":null,'"pub_directions'":null,'"additional_info'":null,'"listedBy'":0,'"listedByName'":null,'"region'":null},{'"id'":438,'"name'":'"Rosehill Public House'",'"address1'":'"Little Petherick'",'"address2'":null,'"towncity'":'"Wadebridge'",'"county'":'"Cornwall'",'"postcode'":'"PL27 7QT'",'"addressVerbose'":'"Little Petherick<br '''/>Wadebridge<br '''/>Cornwall<br '''/>PL27 7QT<br '''/>'",'"addressVerboseLinked'":'"'",'"longitude'":-4.94093,'"latitude'":50.51259,'"londonBorough'":null,'"telno'":'"01841 540 777'",'"faxno'":null,'"email'":null,'"webaddress'":null,'"mainimg'":null,'"venueType'":'"Inn '''/ Pub'",'"description'":null,'"excerpt'":'" '",'"images'":null,'"creationDate'":943920000,'"londonfeatured'":false,'"unusual'":false,'"featured'":false,'"active'":true,'"trial'":false,'"modifiedDate'":1234752874,'"hits'":'"818'",'"allowReviews'":false,'"amenities'":null,'"imagealt'":'"Cornwall wedding reception venue in Wadebridge, Rosehill Public House'",'"imagetitle'":'"Cornwall wedding venues in Wadebridge'",'"car_directions'":null,'"pub_directions'":null,'"additional_info'":null,'"listedBy'":0,'"listedByName'":null,'"region'":null}]"
当我们尝试解码JSON时,这会在网站的其他地方引起一些严重的问题。虽然我们可以使用stripslashes
删除斜杠,但其他一些区域也会导致解析错误(使用第一个PHP文件提供的原始数据时不会出现)。
谁能建议为什么PHP在字符串周围添加斜杠和错误的引号?理想情况下,我们希望在服务器上创建JSON文件时有一个解决方案,而不是从中读取它…
我认为file_get_contents($url)
返回您JSON编码文件?那么你的问题是,你的JSON编码它再次。"[{'"id'":437,'"nam...
是字符串[{"id":1580,"nam...
的正确JSON编码表示。
如果您在读取缓存文件时json_decode
它,您将获得原始JSON字符串。或者干脆不要json_encode
。
相关文章:
- 咕噜声.js无法读取文件(错误代码:EISDIR)
- 在 ubuntu 14.04 VPS 上执行.js文件错误
- 命令代码jquery文件错误
- XMLHttpRequest无法加载Angular js中的文件错误
- 咕噜声/巴别塔错误:无法写入“dist”文件(错误代码:EISDIR)
- 未捕获的类型错误:对象[对象对象]在文件 y 错误中没有方法“x”
- angularjs + 下载 csv 文件 + 错误: [$injector:unpr] 未知提供程序
- 露天更新文件 - 错误开机自检
- Django ExtJs 6:上传文件错误
- Dropbox放入保护程序本地主机文件错误
- 我的网站在提交时出现文件错误
- php生成的JSON文件错误
- JSON文件错误
- 为什么Chrome开发者工具/控制台不显示javascript文件/错误是动态加载的
- 保存JSON文件错误
- Facebook Javascript表单数据照片上传:需要上传文件错误
- 如何在Node.js中上传文件?错误:无法POST/上载
- 外部javascript文件错误
- 使用jQuery表单插件上传文件-无效文件错误
- Json上传jquery文件错误