简体   繁体   English

如何将JsonObject转换为String以存储在Servlet的会话中?

[英]How to convert JsonObject to String for store in session in servlet?

How to convert JsonObject to String for store in session in servlet? 如何将JsonObject转换为String以存储在Servlet的会话中?

JSONObject jSONObject = new JSONObject();

jSONObject.put("user", rs.getString("user").toString());
jSONObject.put("pass", rs.getString("pass").toString());

You can simply use 您可以简单地使用

jSONObject.string();

but if this way doesn't satisfy your needs you can use the following way to convert json object to json string you can use this library in your build gradle 但是如果这种方法不能满足您的需求,则可以使用以下方法将json对象转换为json字符串,您可以在构建gradle中使用该库

compile 'com.google.code.gson:gson:2.7'

and in your code: 并在您的代码中:

String string = new Gson().toJson(jSONObject );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM