質問するログイン新規登録

回答編集履歴

1

詳細に

2017/12/01 07:19

投稿

HW_
HW_

スコア18

answer CHANGED
@@ -1,2 +1,64 @@
1
- 追記したり聞きたいことが何なのか、やりたいことが何なのかわかりづらくなってしまったため、新たに質問したので解決済みとしました。
1
+ ・JSONObject jsonObject = new JSONObject();をfor文内行うことで追記部分を解決
2
+ ・php側も配列に対応できるよう変更
3
+
4
+ 修正したソースを以下に
5
+ ```java
6
+ JSONObject jsonObjectSum = new JSONObject();
7
+ String name;
8
+ for (int i = 0; i < globals.ArrayNum; i++) {
9
+ JSONObject jsonObject = new JSONObject(); //インスタンス化をfor文内で
2
- https://teratail.com/questions/102858
10
+ name = "QR_SOSIN_HIS" + i; //動的にキーを取得
11
+ jsonObject.put("bango_1", globals.bango_1[i]);
12
+ jsonObject.put("bango_2", globals.bango_2[i]);
13
+ jsonObject.put("h_code", globals.h_code[i]);
14
+ jsonObject.put("h_name", globals.h_name[i]);
15
+ jsonObject.put("nyusu", globals.nyusu[i]);
16
+ jsonObject.put("syomi_date", globals.syomi_date[i]);
17
+ jsonObjectSum.put(name, jsonObject); //まとめてputする
18
+ }
19
+
20
+ os = con.getOutputStream();
21
+ BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
22
+ bufferedWriter.write(String.valueOf(jsonObjectSum));
23
+ bufferedWriter.flush();
24
+ bufferedWriter.close();
25
+ ```
26
+
27
+ ```php
28
+ try{
29
+ // データベースへの接続を表すPDOインスタンスを生成
30
+ $pdo = new PDO($dsn,$username,$password);
31
+ $input_str = file_get_contents("php://input");
32
+ $input_json = json_decode($input_str,true);
33
+ $json_count = count($input_json);
34
+
35
+ $bango_1 = array();
36
+ $bango_2 = array();
37
+ $h_code = array();
38
+ $h_name = array();
39
+ $nyusu = array();
40
+ $syomi_date = array();
41
+
42
+
43
+ for($i=0;$i<$json_count;$i++){
44
+ $str = "QR_SOSIN_HIS$i";
45
+ $bango_1[] = $input_json["$str"]["bango_1"];
46
+ $bango_2[] = $input_json["$str"]["bango_2"];
47
+ $h_code[] = $input_json["$str"]["h_code"];
48
+ $h_name[] = $input_json["$str"]["h_name"];
49
+ $nyusu[] = $input_json["$str"]["nyusu"];
50
+ $syomi_date[] = $input_json["$str"]["syomi_date"];
51
+
52
+
53
+ $stmt = $pdo->prepare("INSERT INTO t_kari_send_his (bango_1, bango_2, h_code, h_name, nyusu, syomi_date)
54
+ VALUES(:bango_1, :bango_2, :h_code, :h_name, :nyusu, :syomi_date)");
55
+ $stmt -> bindParam(':bango_1', $bango_1[$i], PDO::PARAM_STR);
56
+ $stmt -> bindParam(':bango_2', $bango_2[$i], PDO::PARAM_STR);
57
+ $stmt -> bindParam(':h_code', $h_code[$i], PDO::PARAM_STR);
58
+ $stmt -> bindParam(':h_name', $h_name[$i], PDO::PARAM_STR);
59
+ $stmt -> bindParam(':nyusu', $nyusu[$i], PDO::PARAM_STR);
60
+ $stmt -> bindParam(':syomi_date', $syomi_date[$i], PDO::PARAM_STR);
61
+ $stmt -> execute();
62
+ }
63
+
64
+ ```