回答編集履歴

1

詳細に

2017/12/01 07:19

投稿

HW_
HW_

スコア18

test CHANGED
@@ -1,3 +1,127 @@
1
- 追記したり聞きたいことが何なのか、やりたいことが何なのかわかりづらくなってしまったため、新たに質問したので解決済みとしました。
1
+ ・JSONObject jsonObject = new JSONObject();をfor文内行うことで追記部分を解決
2
2
 
3
+ ・php側も配列に対応できるよう変更
4
+
5
+
6
+
7
+ 修正したソースを以下に
8
+
9
+ ```java
10
+
11
+ JSONObject jsonObjectSum = new JSONObject();
12
+
13
+ String name;
14
+
15
+ for (int i = 0; i < globals.ArrayNum; i++) {
16
+
17
+ JSONObject jsonObject = new JSONObject(); //インスタンス化をfor文内で
18
+
3
- https://teratail.com/questions/102858
19
+ name = "QR_SOSIN_HIS" + i; //動的にキーを取得
20
+
21
+ jsonObject.put("bango_1", globals.bango_1[i]);
22
+
23
+ jsonObject.put("bango_2", globals.bango_2[i]);
24
+
25
+ jsonObject.put("h_code", globals.h_code[i]);
26
+
27
+ jsonObject.put("h_name", globals.h_name[i]);
28
+
29
+ jsonObject.put("nyusu", globals.nyusu[i]);
30
+
31
+ jsonObject.put("syomi_date", globals.syomi_date[i]);
32
+
33
+ jsonObjectSum.put(name, jsonObject); //まとめてputする
34
+
35
+ }
36
+
37
+
38
+
39
+ os = con.getOutputStream();
40
+
41
+ BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
42
+
43
+ bufferedWriter.write(String.valueOf(jsonObjectSum));
44
+
45
+ bufferedWriter.flush();
46
+
47
+ bufferedWriter.close();
48
+
49
+ ```
50
+
51
+
52
+
53
+ ```php
54
+
55
+ try{
56
+
57
+ // データベースへの接続を表すPDOインスタンスを生成
58
+
59
+ $pdo = new PDO($dsn,$username,$password);
60
+
61
+ $input_str = file_get_contents("php://input");
62
+
63
+ $input_json = json_decode($input_str,true);
64
+
65
+ $json_count = count($input_json);
66
+
67
+
68
+
69
+ $bango_1 = array();
70
+
71
+ $bango_2 = array();
72
+
73
+ $h_code = array();
74
+
75
+ $h_name = array();
76
+
77
+ $nyusu = array();
78
+
79
+ $syomi_date = array();
80
+
81
+
82
+
83
+
84
+
85
+ for($i=0;$i<$json_count;$i++){
86
+
87
+ $str = "QR_SOSIN_HIS$i";
88
+
89
+ $bango_1[] = $input_json["$str"]["bango_1"];
90
+
91
+ $bango_2[] = $input_json["$str"]["bango_2"];
92
+
93
+ $h_code[] = $input_json["$str"]["h_code"];
94
+
95
+ $h_name[] = $input_json["$str"]["h_name"];
96
+
97
+ $nyusu[] = $input_json["$str"]["nyusu"];
98
+
99
+ $syomi_date[] = $input_json["$str"]["syomi_date"];
100
+
101
+
102
+
103
+
104
+
105
+ $stmt = $pdo->prepare("INSERT INTO t_kari_send_his (bango_1, bango_2, h_code, h_name, nyusu, syomi_date)
106
+
107
+ VALUES(:bango_1, :bango_2, :h_code, :h_name, :nyusu, :syomi_date)");
108
+
109
+ $stmt -> bindParam(':bango_1', $bango_1[$i], PDO::PARAM_STR);
110
+
111
+ $stmt -> bindParam(':bango_2', $bango_2[$i], PDO::PARAM_STR);
112
+
113
+ $stmt -> bindParam(':h_code', $h_code[$i], PDO::PARAM_STR);
114
+
115
+ $stmt -> bindParam(':h_name', $h_name[$i], PDO::PARAM_STR);
116
+
117
+ $stmt -> bindParam(':nyusu', $nyusu[$i], PDO::PARAM_STR);
118
+
119
+ $stmt -> bindParam(':syomi_date', $syomi_date[$i], PDO::PARAM_STR);
120
+
121
+ $stmt -> execute();
122
+
123
+ }
124
+
125
+
126
+
127
+ ```