質問編集履歴

2

完成ファイルが出来ました。

2018/12/06 02:27

投稿

zenobread
zenobread

スコア44

test CHANGED
File without changes
test CHANGED
@@ -26,11 +26,11 @@
26
26
 
27
27
  "odpt.Station":"JR-East.Yamanote.Osaki"
28
28
 
29
- }],
29
+ }]
30
30
 
31
31
  }
32
32
 
33
- ],
33
+ ]
34
34
 
35
35
 
36
36
 
@@ -161,3 +161,139 @@
161
161
  と出てきました。
162
162
 
163
163
  ここから先はまだ検証中です。
164
+
165
+
166
+
167
+ 追記2
168
+
169
+ 下のご回答者様の助力を得て
170
+
171
+ 1、jsonファイルを読み込む方法
172
+
173
+ 2、jsonファイルをphp内に埋め込む方法
174
+
175
+ 2つで行う方法が分かりました。
176
+
177
+ 以下はそのコードです。(jsonファイルは修正済みです)
178
+
179
+
180
+
181
+
182
+
183
+ ```APItest.php
184
+
185
+ <?php
186
+
187
+ $url = "http://localhost/php/APItest.json";
188
+
189
+ $json = file_get_contents($url);
190
+
191
+ $json = mb_convert_encoding($json,'UTF8','ASCII,JIS,UTF=8,EUC-JP,SJIS-WIN');
192
+
193
+ $arr = json_decode($json,true);
194
+
195
+
196
+
197
+ if($arr==NULL){
198
+
199
+ echo "MISS!";
200
+
201
+ return;
202
+
203
+ }else{
204
+
205
+ $json_count=count($arr["res"]["odpt:stationTimetableObject"]);
206
+
207
+ $train=array();
208
+
209
+ $traintype=array();
210
+
211
+ $number=array();
212
+
213
+ $time=array();
214
+
215
+ $station=array();
216
+
217
+ for($i=$json_count-1;$i>=0;$i--){
218
+
219
+ $train[]=$arr["res"]["odpt:stationTimetableObject"][$i]["odpt:train"];
220
+
221
+ $traintype[]=$arr["res"]["odpt:stationTimetableObject"][$i]["odpt:trainType"];
222
+
223
+ $number[]=$arr["res"]["odpt:stationTimetableObject"][$i]["odpt:trainNumber"];
224
+
225
+ $time[]=$arr["res"]["odpt:stationTimetableObject"][$i]["odpt:departureTime"];
226
+
227
+ $dest[]=$arr["res"]["odpt:stationTimetableObject"][$i]["odpt:destinationStation"][0]["odpt.Station"];
228
+
229
+ }
230
+
231
+ }
232
+
233
+ echo $dest[0];
234
+
235
+ //以下はjson内のエラーを発見するメッセージ関数なので記入しないでも大丈夫です
236
+
237
+ echo json_last_error();
238
+
239
+ echo json_last_error_msg();
240
+
241
+ ?>
242
+
243
+ //echo 結果
244
+
245
+ //JR-East.Yamanote.Osaki0No error
246
+
247
+ ```
248
+
249
+
250
+
251
+ ```APItest2.php
252
+
253
+ <?php
254
+
255
+ $j = '{"res":
256
+
257
+ {
258
+
259
+ "odpt:stationTimetableObject":[
260
+
261
+ {
262
+
263
+ "odpt:train":"odpt.Train:JR-East.Yamanote.350G",
264
+
265
+ "odpt:trainType":"odpt.TrainType:JR-East.Local",
266
+
267
+ "odpt:trainNumber":"350G",
268
+
269
+ "odpt:departureTime":"04:34",
270
+
271
+ "odpt:destinationStation":[{
272
+
273
+ "odpt.Station":"JR-East.Yamanote.Osaki"
274
+
275
+ }]
276
+
277
+ }
278
+
279
+ ]
280
+
281
+ }
282
+
283
+ }';
284
+
285
+ $res = json_decode('http://localhost/php/APItest.json',true);
286
+
287
+ $odpt_station="odpt:stationTimetableObject";
288
+
289
+ $odpt_train="odpt:train";
290
+
291
+ echo json_last_error();
292
+
293
+ echo json_last_error_msg();
294
+
295
+ echo $res->res->$odpt_station[0]->$odpt_train;
296
+
297
+ ?>
298
+
299
+ ```

1

エラーの原因がjson_decodeするときにあるのではと考えました。

2018/12/06 02:27

投稿

zenobread
zenobread

スコア44

test CHANGED
File without changes
test CHANGED
@@ -48,11 +48,43 @@
48
48
 
49
49
  $json = file_get_contents($url);
50
50
 
51
+ /*
52
+
53
+ for($n=0;$n<=31;++$n){
54
+
55
+ $json=str_replace(chr(127),"",$json);
56
+
57
+ }
58
+
59
+ $json=str_replace(chr(127),"",$json);
60
+
61
+ if(0===strpos(bin2hex($json),'efbbbf')){
62
+
63
+ $json=substr($json,3);
64
+
65
+ }
66
+
67
+ */
68
+
51
69
  $json = mb_convert_encoding($json,'UTF8','ASCII,JIS,UTF=8,EUC-JP,SJIS-WIN');
52
70
 
53
- $arr = json_decode($json,true);
71
+ $arr = json_decode($json);
72
+
73
+ $result=var_export($arr,true);
74
+
75
+ echo $result;
76
+
77
+ echo json_last_error();
78
+
79
+ echo json_last_error_msg();
80
+
81
+ print_r($json);
82
+
83
+
54
84
 
55
85
  if($arr==NULL){
86
+
87
+ echo "MISS!";
56
88
 
57
89
  return;
58
90
 
@@ -107,3 +139,25 @@
107
139
  参考サイトはこちらです
108
140
 
109
141
  https://qiita.com/fantm21/items/603cbabf2e78cb08133e
142
+
143
+
144
+
145
+ 追記1
146
+
147
+ そもそも$arr===NULLのところで失敗してるようで
148
+
149
+ echo MISS! の文が実行されました。
150
+
151
+ いろいろしらべてJsonで何が間違っているかエラー文を出す
152
+
153
+ echo json_last_error();
154
+
155
+ echo json_last_error_msg();
156
+
157
+ を行ってみたところ
158
+
159
+ 4Syntax error
160
+
161
+ と出てきました。
162
+
163
+ ここから先はまだ検証中です。