回答編集履歴
6
見直し
answer
CHANGED
@@ -20,8 +20,7 @@
|
|
20
20
|
//file_put_contents('226112_data.json', $json);
|
21
21
|
//exit;
|
22
22
|
$json = file_get_contents('226112_data.json');
|
23
|
-
$
|
23
|
+
$json_arr = json_decode(preg_replace('/^\xEF\xBB\xBF/', '', $json), true);
|
24
|
-
$json_arr = json_decode($json, true);
|
25
24
|
if (is_null($json_arr)) {
|
26
25
|
echo json_last_error() . PHP_EOL;
|
27
26
|
echo json_last_error_msg() . PHP_EOL;
|
@@ -39,7 +38,6 @@
|
|
39
38
|
|
40
39
|
```
|
41
40
|
こんな感じに雑サンプルコード。
|
42
|
-
直接取得したjsonファイルを処理するとSyntax error
|
41
|
+
直接取得したjsonファイルを処理するとSyntax errorになりますが、
|
43
|
-
ネット上のjson整形サービスにて加工したものならエラーなく実行できる、
|
44
|
-
|
42
|
+
UTF-8のBOM付きであるためなので、
|
45
|
-
|
43
|
+
preg_replaceを組み合わせれば回避できます。
|
5
見直し
answer
CHANGED
@@ -19,7 +19,7 @@
|
|
19
19
|
//$json = file_get_contents($url);
|
20
20
|
//file_put_contents('226112_data.json', $json);
|
21
21
|
//exit;
|
22
|
-
$json = file_get_contents('
|
22
|
+
$json = file_get_contents('226112_data.json');
|
23
23
|
$json = mb_convert_encoding($json, 'UTF-8');
|
24
24
|
$json_arr = json_decode($json, true);
|
25
25
|
if (is_null($json_arr)) {
|
@@ -39,6 +39,7 @@
|
|
39
39
|
|
40
40
|
```
|
41
41
|
こんな感じに雑サンプルコード。
|
42
|
-
|
42
|
+
直接取得したjsonファイルを処理するとSyntax errorを回避できず、
|
43
|
-
|
43
|
+
ネット上のjson整形サービスにて加工したものならエラーなく実行できる、
|
44
|
-
|
44
|
+
というところまで動作確認を取ったものの、
|
45
|
+
今日は一旦ここで区切ります。
|
4
見直し
answer
CHANGED
@@ -8,18 +8,37 @@
|
|
8
8
|
|
9
9
|
---
|
10
10
|
```php
|
11
|
+
<html>
|
12
|
+
<head>
|
13
|
+
<title>226112.php</title>
|
14
|
+
</head>
|
15
|
+
<body>
|
11
16
|
<?php
|
12
17
|
|
13
18
|
//$url = "https://books.rakuten.co.jp/event/book/pocket-book/calendar/2019/11/js/booklist.json"; //新刊検索
|
14
19
|
//$json = file_get_contents($url);
|
20
|
+
//file_put_contents('226112_data.json', $json);
|
21
|
+
//exit;
|
15
|
-
$json = file_get_contents('
|
22
|
+
$json = file_get_contents('226112_data_s.json');
|
23
|
+
$json = mb_convert_encoding($json, 'UTF-8');
|
16
|
-
$json_arr = json_decode($json,
|
24
|
+
$json_arr = json_decode($json, true);
|
25
|
+
if (is_null($json_arr)) {
|
26
|
+
echo json_last_error() . PHP_EOL;
|
17
|
-
|
27
|
+
echo json_last_error_msg() . PHP_EOL;
|
28
|
+
exit;
|
29
|
+
}
|
18
30
|
$list = $json_arr['list'];
|
19
31
|
echo "<p>" . PHP_EOL;
|
20
32
|
foreach ($list as list($id,$url,$img,$eisbn,$date,$ttl,$ttl_kana,$athr,$dprc,$bprc,$ahid_id,$jun_id,$cpn_id,$ser_id,$lbl_id,$s_rec,$s_new,$s_res,$s_frc,$f_adlt,$fc1_id,$fc2_id,$fc3_id,$viewer_id,$viewer_url)) {
|
21
33
|
echo $ttl . "<br />" . PHP_EOL;
|
22
34
|
}
|
23
35
|
echo "</p>" . PHP_EOL;
|
36
|
+
?>
|
37
|
+
</body>
|
38
|
+
</html>
|
39
|
+
|
24
40
|
```
|
25
|
-
こんな感じに雑サンプルコード。
|
41
|
+
こんな感じに雑サンプルコード。
|
42
|
+
テストに使ったjsonファイルの保存方法に問題があったようだったので、
|
43
|
+
改めてphpコード内でfile_put_contents()にて保存したjsonファイルを基に動作確認をとった次第。
|
44
|
+
(mb_convert_encoding()でUTF-8へ変換かけたら、json_decode()でSyntax errorがでなくなった、っていう。)
|
3
見直し
answer
CHANGED
@@ -7,7 +7,7 @@
|
|
7
7
|
雑に扱うと良くない気がします。
|
8
8
|
|
9
9
|
---
|
10
|
-
|
10
|
+
```php
|
11
11
|
<?php
|
12
12
|
|
13
13
|
//$url = "https://books.rakuten.co.jp/event/book/pocket-book/calendar/2019/11/js/booklist.json"; //新刊検索
|
2
見直し
answer
CHANGED
@@ -4,4 +4,22 @@
|
|
4
4
|
|
5
5
|
老婆心ながら、
|
6
6
|
楽天ブックスの新刊カレンダーのjsonは非公式APIのようなので、
|
7
|
-
雑に扱うと良くない気がします。
|
7
|
+
雑に扱うと良くない気がします。
|
8
|
+
|
9
|
+
---
|
10
|
+
'''php
|
11
|
+
<?php
|
12
|
+
|
13
|
+
//$url = "https://books.rakuten.co.jp/event/book/pocket-book/calendar/2019/11/js/booklist.json"; //新刊検索
|
14
|
+
//$json = file_get_contents($url);
|
15
|
+
$json = file_get_contents('226112_data.json');
|
16
|
+
$json_arr = json_decode($json, TRUE);
|
17
|
+
//print_r($json_arr['column']);
|
18
|
+
$list = $json_arr['list'];
|
19
|
+
echo "<p>" . PHP_EOL;
|
20
|
+
foreach ($list as list($id,$url,$img,$eisbn,$date,$ttl,$ttl_kana,$athr,$dprc,$bprc,$ahid_id,$jun_id,$cpn_id,$ser_id,$lbl_id,$s_rec,$s_new,$s_res,$s_frc,$f_adlt,$fc1_id,$fc2_id,$fc3_id,$viewer_id,$viewer_url)) {
|
21
|
+
echo $ttl . "<br />" . PHP_EOL;
|
22
|
+
}
|
23
|
+
echo "</p>" . PHP_EOL;
|
24
|
+
```
|
25
|
+
こんな感じに雑サンプルコード。
|
1
加筆修正
answer
CHANGED
@@ -1,3 +1,7 @@
|
|
1
1
|
[PHP: print_r - Manual](https://www.php.net/manual/ja/function.print-r.php)
|
2
2
|
|
3
|
-
`print_r($json_arr);`
|
3
|
+
`print_r($json_arr);`
|
4
|
+
|
5
|
+
老婆心ながら、
|
6
|
+
楽天ブックスの新刊カレンダーのjsonは非公式APIのようなので、
|
7
|
+
雑に扱うと良くない気がします。
|