我有一个存储数据库数据的HashMap如何将哈希映射输入到JSONObject中?
HashMap<AppDataRequest, AppData> appdatas = new HashMap<AppDataRequest, AppData>();
public AppDataService(){
Connection conn = null;
Statement stat = null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stat = conn.createStatement();
String sql = "SELECT * FROM testdata";
ResultSet resu = stat.executeQuery(sql);
while(resu.next()){
int id = resu.getInt("app_id");
String email = resu.getString("email");
String password = resu.getString("password");
String token = resu.getString("token");
appdatas.put(new AppDataRequest(id, email, password), new AppData(" ", "success", token));
}
resu.close();
stat.close();
conn.close();
}
如何将HashMap appdatas放入JSONObject?我尝试将HashMap放入JSONObject,因为我希望实现如下输出。
{
"AppData": {
"status": "success",
"message": ""
"token": "****"
}
}
提前感谢您的回复。
最佳答案
使用GSON
下载以下Google的APIGson
String json=new Gson().toJson(appdatas);
关于java - 将Hashmap <Object,Object>输入到JSONObject,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43386382/