2011-12-14 8 views
0

現在、PHPスクリプトを使用してJSONファイルのキャッシュを作成しています。私たちはサーバー上に非常に大きなデータベースを照会し、結果セットをJSON形式で返すPHPファイルを用意しています(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ファイルを作成する時点で回避することをお勧めします。

+2

なぜあなたはJSONで文字列をエンコードするのですか? – svens

+1

svensが言ったように..なぜあなたはJSON文字列をエンコードしていますか?あなたは同じ仕事を2回やっています。あなたのfwriteからjson_encodeを削除するだけです。 –

答えて

1

私はそれを受け取りますfile_get_contents($url)はJSONエンコードファイルを返しますか?あなたの問題は、それをエンコードしているJSONだということです。"[{\"id\":437,\"nam...は、文字列[{"id":1580,"nam...の適切なJSONエンコード表現です。

キャッシュファイルから読み取るときにjson_decodeと書いた場合、オリジナルのJSON文字列が返されます。それとも、json_encodeしないでください。

+0

ありがとうございます。時には、全体的に目の第二のペアを得るのに役立ちます。 – BenM

関連する問題