Java 使用JSON的数组中的数组
这是JAVA中的代码,我使用了JSONArray和方法Java 使用JSON的数组中的数组,java,arrays,json,Java,Arrays,Json,这是JAVA中的代码,我使用了JSONArray和方法JSONArray.put(字符串) 这是我的输出 ['Command','Book Issued'],["Central",324,"Southern",312,"South-West",192,"Eastern",264,"Northern",84,"Western",396], 但我希望实际输出如下: [ ['Command', 'Books Issued'], ["Central",324],["Southern",
JSONArray.put(字符串)代码>
这是我的输出
['Command','Book Issued'],["Central",324,"Southern",312,"South-West",192,"Eastern",264,"Northern",84,"Western",396],
但我希望实际输出如下:
[
['Command', 'Books Issued'],
["Central",324],["Southern",312],
["South-West",192],
["Eastern",264],
["Northern",84],
["Western",396]
]
这些数据在谷歌图表中用于绘制条形图。AJSONArray
不限于字符串
您需要创建一个用于保存记录的数组,然后为每对或多条记录创建一个新数组。基本思路如下:
// create the "top" array
JSONArray topArray = new JSONArray();
// add your static "headers"
JSONArray headers = new JSONArray();
headers.put("Command");
headers.put("Book Issued");
topArray.put(headers);
while (rs.next()){
// create a new array for the current record
JSONArray recordArray = new JSONArray();
// populate the record array
String zone = rs.getString("command");
recordArray.put(zone);
int booksissued = rs.getInt("books_issued");
recordArray.put(booksissued);
// append the record array to the top array
topArray.put(recordArray);
}
return topArray;
Javascript
!=<代码>Java
感谢Derlin先生的快速回复。。。实际上输出是这样的:[[“中部”,324],“南部”,312],“西南部”,192],“东部”,264],“北部”,84],“西部”,396],
我想要这样的输出:[[“命令”,“发行的书”],[“中部”,324],“南部”,312],“西南部”,192],“东部”,264],[“Northern”,84],“Western”,396]]
谢谢如果只缺少第一个数组,请将其添加到之前,而(请参阅更新的答案)。(顺便说一句,是德林小姐)
// create the "top" array
JSONArray topArray = new JSONArray();
// add your static "headers"
JSONArray headers = new JSONArray();
headers.put("Command");
headers.put("Book Issued");
topArray.put(headers);
while (rs.next()){
// create a new array for the current record
JSONArray recordArray = new JSONArray();
// populate the record array
String zone = rs.getString("command");
recordArray.put(zone);
int booksissued = rs.getInt("books_issued");
recordArray.put(booksissued);
// append the record array to the top array
topArray.put(recordArray);
}
return topArray;