2016-03-24 9 views
-2

次の出力があります。私はそれをPHPを使用して読んでみたい。 私は?(を開始および終了);除き、JSONようphpを使用してデータを個別フォーマットで表示

 ?({"ip":"104.112.115.28","country_code":"US","country_name":"United States","region_code":"CA","region_name":"California","city":"Houston","zip_code":"90013","time_zone":"America/Los_Angeles","latitude":34.0453,"longitude":-118.2413,"metro_code":803}); 
+0

JSONとほとんど同じように見えるので、json_decodeをほとんど使用することができます。 –

+0

この出力をどのように取得しているかを示す情報を少し追加できますか? –

+0

$ decodedJson = json_decode($ data、true); foreach($ decodedJson $値){ echo $ value ['region_name']; エコー "
"; } – Parag

答えて

1

さて、あなたが持っているものに見えることを行うことができますどのように教えてください。それらを削除すると、json_decodeを使用して文字列をPHP stdClassオブジェクトにデコードし、その値にアクセスできます。 、それは大まかな答えだ再び

$result = ' ?({"ip":"104.112.115.28","country_code":"US","country_name":"United States","region_code":"CA","region_name":"California","city":"Houston","zip_code":"90013","time_zone":"America/Los_Angeles","latitude":34.0453,"longitude":-118.2413,"metro_code":803});'; 

//remove the bad stuff on the start and end, which assumes these strings don't exist elsewhere in the result, which is a pretty big assumption 
$string = str_replace('?({', '{', str_replace('});', '}', trim($result)); 

$data = json_decode($string); 

echo $data->ip; 

と大きな前提になりますが、それはおそらく、ほとんどのケースの下で動作します;:

ぞんざいに行われ、あなたのような何かを試みることができます確かにあなたが与えた例のために。

+0

ありがとうございます。それは – Parag

+0

@パラグ、それを聞いてうれしい!あなたは答えとしてそれを受け入れることができますか?ありがとう!楽しむ。 – Davis

関連する問題