jsonデータをphpで取り出して画面上に表示させたいのですが
エラーは出なくなったのにChromeブラウザで全く何も表示されません.
何か間違っているところはありますか?以下はそのコードと参考にしたサイト様です。
APItest.json
1{"res": 2 { 3 "odpt:stationTimetableObject":[ 4 { 5 "odpt:train":"odpt.Train:JR-East.Yamanote.350G", 6 "odpt:trainType":"odpt.TrainType:JR-East.Local", 7 "odpt:trainNumber":"350G", 8 "odpt:departureTime":"04:34", 9 "odpt:destinationStation":[{ 10 "odpt.Station":"JR-East.Yamanote.Osaki" 11 }] 12 } 13 ] 14 15 } 16}
APItest.php
1<?php 2$url = "http://localhost/php/APItest.json"; 3$json = file_get_contents($url); 4/* 5for($n=0;$n<=31;++$n){ 6 $json=str_replace(chr(127),"",$json); 7} 8$json=str_replace(chr(127),"",$json); 9if(0===strpos(bin2hex($json),'efbbbf')){ 10 $json=substr($json,3); 11} 12*/ 13$json = mb_convert_encoding($json,'UTF8','ASCII,JIS,UTF=8,EUC-JP,SJIS-WIN'); 14$arr = json_decode($json); 15$result=var_export($arr,true); 16echo $result; 17echo json_last_error(); 18echo json_last_error_msg(); 19print_r($json); 20 21if($arr==NULL){ 22 echo "MISS!"; 23 return; 24}else{ 25 $json_count=count($arr["res"]["odpt:stationTimetableObject"]); 26 $train=array(); 27 $traintype=array(); 28 $number=array(); 29 $time=array(); 30 $station=array(); 31 echo $json_count; 32 for($i=$json_count-1;$i>=0;$i--){ 33 $train[]=$arr["res"]["odpt:stationTimetableObject"][$i]["odpt:train"]; 34 $traintype[]=$arr["res"]["odpt:stationTimetableObject"][$i]["odpt:trainType"]; 35 $number[]=$arr["res"]["odpt:stationTimetableObject"][$i]["odpt:trainNumber"]; 36 $time[]=$arr["res"]["odpt:stationTimetableObject"][$i]["odpt:departureTime"]; 37 $time[]=$arr["res"]["odpt:stationTimetableObject"][$i]["odpt:destinationStation"][0]["odpt.Station:JR-East.Yamanote.Osaki"]; 38 39 } 40} 41echo $json_count; 42?>
とりあえずきちんと処理できているか試そうとechoで$json_countが動いているか
最後の行や途中のif文内で試しています
参考サイトはこちらです
https://qiita.com/fantm21/items/603cbabf2e78cb08133e
追記1
そもそも$arr===NULLのところで失敗してるようで
echo MISS! の文が実行されました。
いろいろしらべてJsonで何が間違っているかエラー文を出す
echo json_last_error();
echo json_last_error_msg();
を行ってみたところ
4Syntax error
と出てきました。
ここから先はまだ検証中です。
追記2
下のご回答者様の助力を得て
1、jsonファイルを読み込む方法
2、jsonファイルをphp内に埋め込む方法
2つで行う方法が分かりました。
以下はそのコードです。(jsonファイルは修正済みです)
APItest.php
1<?php 2$url = "http://localhost/php/APItest.json"; 3$json = file_get_contents($url); 4$json = mb_convert_encoding($json,'UTF8','ASCII,JIS,UTF=8,EUC-JP,SJIS-WIN'); 5$arr = json_decode($json,true); 6 7if($arr==NULL){ 8 echo "MISS!"; 9 return; 10}else{ 11 $json_count=count($arr["res"]["odpt:stationTimetableObject"]); 12 $train=array(); 13 $traintype=array(); 14 $number=array(); 15 $time=array(); 16 $station=array(); 17 for($i=$json_count-1;$i>=0;$i--){ 18 $train[]=$arr["res"]["odpt:stationTimetableObject"][$i]["odpt:train"]; 19 $traintype[]=$arr["res"]["odpt:stationTimetableObject"][$i]["odpt:trainType"]; 20 $number[]=$arr["res"]["odpt:stationTimetableObject"][$i]["odpt:trainNumber"]; 21 $time[]=$arr["res"]["odpt:stationTimetableObject"][$i]["odpt:departureTime"]; 22 $dest[]=$arr["res"]["odpt:stationTimetableObject"][$i]["odpt:destinationStation"][0]["odpt.Station"]; 23 } 24} 25echo $dest[0]; 26//以下はjson内のエラーを発見するメッセージ関数なので記入しないでも大丈夫です 27echo json_last_error(); 28echo json_last_error_msg(); 29?> 30//echo 結果 31//JR-East.Yamanote.Osaki0No error
APItest2.php
1<?php 2$j = '{"res": 3 { 4 "odpt:stationTimetableObject":[ 5 { 6 "odpt:train":"odpt.Train:JR-East.Yamanote.350G", 7 "odpt:trainType":"odpt.TrainType:JR-East.Local", 8 "odpt:trainNumber":"350G", 9 "odpt:departureTime":"04:34", 10 "odpt:destinationStation":[{ 11 "odpt.Station":"JR-East.Yamanote.Osaki" 12 }] 13 } 14 ] 15 } 16}'; 17$res = json_decode('http://localhost/php/APItest.json',true); 18$odpt_station="odpt:stationTimetableObject"; 19$odpt_train="odpt:train"; 20echo json_last_error(); 21echo json_last_error_msg(); 22echo $res->res->$odpt_station[0]->$odpt_train; 23?>

回答2件
あなたの回答
tips
プレビュー