I'm a little confused on how to construct associative arrays from a hashtable in java with either JSONObject() or the gson library from google.
Any help on this is appreciated!
JSONObject message = new JSONObject();
Map<String,String> responseData = new Hashtable<String, String>();
[...]
ResultSet results = getApprovalCount.executeQuery();
while (results.next()) {
responseData.put("vote" + results.getString("submission_id"), results.getString("counter"));
}
[...]
message.put("submissions", responseData);
Result:
{"submissions":{"vote1":"2","vote7":"1","vote25":"1","vote6":"1","vote13":"1","vote9":"1","vote11":"1"}}
Desired result:
{"submissions":[{"vote1":"2"},{"vote7":"1"},{"vote25":"1"},{"vote6":"1"},{"vote13":"1"},{"vote9":"1"},{"vote11":"1"}]}
Figured it out...!
JSONObject message = new JSONObject();
ArrayList<Map<String,String>> responseData = new ArrayList<Map<String,String>>();
User user = new User(request);
[...]
while (results.next()) {
Map<String,String> tmpdata = new Hashtable<String, String>();
tmpdata.put("vote" + results.getString("submission_id"), results.getString("counter"));
responseData.add(tmpdata);
}
[...]
message.put("submissions", responseData);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.