php生成的JSON文件错误

PHP-generarted JSON File Errors

本文关键字:文件 错误 JSON php      更新时间:2023-09-26

我目前正在使用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