下記のようなJsonコードがあるとして、phpで扱うためにはどうしたらよろしいでしょうか?
Jsonあまり触ったことないのでメンバへのアクセスがイマイチわからないと言いますか...
if(typeof(xml)=='undefined') xml = {}; xml.data = {"line_cd":11302,"line_name":"JR山手線","line_lon":139.73522275686264,"line_lat":35.69302730762992,"line_zoom":12,"station_l":[{"station_cd":1130201,"station_g_cd":1130201,"station_name":"大崎","lon":139.728439,"lat":35.619772},{"station_cd":1130202,"station_g_cd":1130202,"station_name":"五反田","lon":139.723822,"lat":35.625974},{"station_cd":1130203,"station_g_cd":1130203,"station_name":"目黒","lon":139.715775,"lat":35.633923},{"station_cd":1130204,"station_g_cd":1130204,"station_name":"恵比寿","lon":139.71007,"lat":35.646685},{"station_cd":1130205,"station_g_cd":1130205,"station_name":"渋谷","lon":139.701238,"lat":35.658871},{"station_cd":1130206,"station_g_cd":1130206,"station_name":"原宿","lon":139.702592,"lat":35.670646},{"station_cd":1130207,"station_g_cd":1130207,"station_name":"代々木","lon":139.702042,"lat":35.683061},{"station_cd":1130208,"station_g_cd":1130208,"station_name":"新宿","lon":139.700464,"lat":35.689729},{"station_cd":1130209,"station_g_cd":1130209,"station_name":"新大久保","lon":139.700261,"lat":35.700875},{"station_cd":1130210,"station_g_cd":1130210,"station_name":"高田馬場","lon":139.703715,"lat":35.712677},{"station_cd":1130211,"station_g_cd":1130211,"station_name":"目白","lon":139.706228,"lat":35.720476},{"station_cd":1130212,"station_g_cd":1130212,"station_name":"池袋","lon":139.711086,"lat":35.730256},{"station_cd":1130213,"station_g_cd":1130213,"station_name":"大塚","lon":139.728584,"lat":35.731412},{"station_cd":1130214,"station_g_cd":1130214,"station_name":"巣鴨","lon":139.739303,"lat":35.733445},{"station_cd":1130215,"station_g_cd":1130215,"station_name":"駒込","lon":139.748053,"lat":35.736825},{"station_cd":1130216,"station_g_cd":1130216,"station_name":"田端","lon":139.761229,"lat":35.737781},{"station_cd":1130217,"station_g_cd":1130217,"station_name":"西日暮里","lon":139.766857,"lat":35.731954},{"station_cd":1130218,"station_g_cd":1130218,"station_name":"日暮里","lon":139.771287,"lat":35.727908},{"station_cd":1130219,"station_g_cd":1130219,"station_name":"鶯谷","lon":139.778015,"lat":35.721484},{"station_cd":1130220,"station_g_cd":1130220,"station_name":"上野","lon":139.777043,"lat":35.71379},{"station_cd":1130221,"station_g_cd":1130221,"station_name":"御徒町","lon":139.774727,"lat":35.707282},{"station_cd":1130222,"station_g_cd":1130222,"station_name":"秋葉原","lon":139.773288,"lat":35.698619},{"station_cd":1130223,"station_g_cd":1130223,"station_name":"神田","lon":139.770641,"lat":35.691173},{"station_cd":1130224,"station_g_cd":1130101,"station_name":"東京","lon":139.766103,"lat":35.681391},{"station_cd":1130225,"station_g_cd":1130225,"station_name":"有楽町","lon":139.763806,"lat":35.675441},{"station_cd":1130226,"station_g_cd":1130102,"station_name":"新橋","lon":139.758587,"lat":35.666195},{"station_cd":1130227,"station_g_cd":1130227,"station_name":"浜松町","lon":139.757135,"lat":35.655391},{"station_cd":1130228,"station_g_cd":1130228,"station_name":"田町","lon":139.747575,"lat":35.645736},{"station_cd":1130229,"station_g_cd":1130103,"station_name":"品川","lon":139.738999,"lat":35.62876}]} if(typeof(xml.onload)=='function') xml.onload(xml.data);
xmlもあまり扱ったことないですが、こちらは感覚的にわかりました。
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>サンプル</title> </head> <body> <?php $req = "http://www.ekidata.jp/api/l/11302.xml"; $xml = simplexml_load_file($req) or die("XMLパースエラー"); echo "鉄道線名:".$xml->line->line_name."<br>"; echo "軽度:".$xml->line->line_lon."<br>"; echo "経緯:".$xml->line->line_lat."<br>"; ?> <table> <thead> <tr> <th>駅名</th> <th>軽度</th> <th>経緯</th> </tr> </thead> <tbody> <?php for($i=0;$i<count($xml->station);$i++){ echo "<tr>"; echo "<td>".$xml->station[$i]->station_name."</td>"; echo "<td>".$xml->station[$i]->lon."</td>"; echo "<td>".$xml->station[$i]->lat."</td>"; echo "</tr>"; } ?> </tbody> </table> <?php //$xmlオブジェクトの中身を確認する場合は、以下のコメントを外す echo "<pre>"; print_r ($xml); echo "</pre>"; ?> </body> </html>
Jsonも途中まで試していますがなかなか取り出せないです。
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>サンプル</title> </head> <body> <?php $url = "http://www.ekidata.jp/api/l/11302.json"; $json = file_get_contents($url); $json = mb_convert_encoding($json, 'UTF8', 'ASCII,JIS,UTF-8,EUC-JP,SJIS-WIN'); $arr = json_decode($json,true); echo "鉄道線名:".$json->line_name."<br>"; echo "軽度:".$xml->line->line_lon."<br>"; echo "経緯:".$xml->line->line_lat."<br>"; ?> <table> <thead> <tr> <th>駅名</th> <th>軽度</th> <th>経緯</th> </tr> </thead> <tbody> <?php for($i=0;$i<count($xml->station);$i++){ echo "<tr>"; echo "<td>".$xml->station[$i]->station_name."</td>"; echo "<td>".$xml->station[$i]->lon."</td>"; echo "<td>".$xml->station[$i]->lat."</td>"; echo "</tr>"; } ?> </tbody> </table> <?php //$xmlオブジェクトの中身を確認する場合は、以下のコメントを外す echo "<pre>"; print_r ($json); echo "</pre>"; ?> </body> </html>
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/09/01 09:33