我目前使用PHP腳本來創建JSON文件的緩存。我們的服務器上有一個PHP文件,用於查詢非常大的數據庫,並以JSON格式返回結果集(所以我們可以在jQuery和其他框架中在網站上使用它)。PHP生成的JSON文件錯誤
從我們的數據庫中呈現原始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文件時解決問題,而不是從中讀取JSON文件...
爲什麼地獄你JSON編碼的字符串? – svens 2011-12-14 10:07:53
像svens說..你爲什麼編碼JSON字符串?你正在做兩次相同的工作。只需從你的fwrite中刪除json_encode即可。 – 2011-12-14 10:12:26