这是我当前的
[{
"year": "2015",
"org_name": "MOOE",
"suborg_name": "Forms Expenses",
"amt": "2",
"org_id": "4",
"suborg_id": "24"
}, {
"year": "2016",
"org_name": "MOOE",
"suborg_name": "Forms Expenses",
"amt": "1",
"org_id": "4",
"suborg_id": "24"
}]
我想要这个格式
[{
"org_name": "MOOE",
"suborg_name": "Forms Expenses",
"suborg_id": "24",
"org_id": "4",
"Year": [{
"year": "2015",
"amt": "2"
}, {
"year": "2016",
"amt": "1"
}]
}]
下面是我获取当前值的方法:
List<String> list = new ArrayList<String>();
while (rs2.next()) {
JSONObject json = new JSONObject();
if (!rs2.getString("suborg_id").isEmpty()) {
json.put("org_name", org_name);
json.put("year", year);
json.put("suborg_name", suborg_name);
json.put("amt", amt);
json.put("org_id", org_id);
json.put("suborg_id", suborg_id);
}
finaljson2.put(counter2, json.toString());
counter2++;
list.add("" + json + "");
}
//out.println("[" + finaljson2.toString() + "]");
out.println(list);
我放入if语句,希望它将相同的suborg_id
分组
UPDATE
尝试在推入json之前将年份和金额添加到新数组中,如
List<String> list = new ArrayList<String>();
while (rs2.next()) {
JSONObject json = new JSONObject();
JSONObject json1 = new JSONObject();
if (!rs2.getString("suborg_id").isEmpty()) {
json.put("year", year);
json.put("amt", amt);
json.put("org_name", org_name);
json.put("suborg_name", suborg_name);
json.put("org_id", org_id);
json.put("suborg_id", suborg_id);
json.put("year", json1);
}
finaljson2.put(counter2, json.toString());
counter2++;
list.add("" + json + "");
}
//out.println("[" + finaljson2.toString() + "]");
out.println(list);
输出如下:
[{
"org_name": "MOOE",
"suborg_name": "Forms Expenses",
"org_id": "4",
"suborg_id": "24",
"year": {
"year": "2015",
"amt": "2"
}
}, {
"org_name": "MOOE",
"suborg_name": "Forms Expenses",
"org_id": "4",
"suborg_id": "24",
"year": {
"year": "20165",
"amt": "1"
}
}]
我的目标是让格式
[{
"org_name": "MOOE",
"suborg_name": "Forms Expenses",
"suborg_id": "24",
"org_id": "4",
"Year": [{
"year": "2015",
"amt": "2"
}, {
"year": "2016",
"amt": "1"
}]
}]
试试这个
List<String> list = new ArrayList<String>();
while (rs2.next()) {
JSONObject json = new JSONObject();
if (!rs2.getString("suborg_id").isEmpty()) {
json.put("org_name", org_name);
json.put("suborg_name", suborg_name);
json.put("org_id", org_id);
json.put("suborg_id", suborg_id);
json.put("year");
[{
json.put("year", year);
json.put("amt", amt);
},{
json.put("year", year);
json.put("amt", amt);
}]
}
finaljson2.put(counter2, json.toString());
counter2++;
list.add("" + json + "");
}