我想读取和写入一个Json对象到我的数据库,我没有得到如何将您从数据库中获得的字符串转换为字符串,以及如何使用它稍后tp写回再次将列表更改为我的数据库。Convertig Json到列表
所以,当我要求我要领域的数据库中,我得到这个字符串返回:
["[\"pw1\",\"pw2\",\"pw3\"]"]
然后我去创造我的类的对象LastPasswords
List<String> passwordList = (List<String>) controllerServlet.getMacroDatabaseManager().executeNativeQuery(queryGet);
LastPasswords lastPasswords = new LastPasswords(passwordList);
Gson gson = new Gson();
String Json = gson.toJson(lastPasswords);
这里是LastPasswords
类
public class LastPasswords {
private List<String> passwords;
public LastPasswords(List<String> passwords) {
this.passwords = passwords;
}
public List<String> getPasswords(){
return passwords;
}
public void setPasswords(List<String> passwords){
this.passwords = passwords;
}
}
然后,当我有这个json字符串我t把它作为一个清单,但我没有得到清单。
lastPasswords.setPasswords((List<String>) gson.fromJson(banana, LastPasswords.class));
passwordList = lastPasswords.getPasswords();
感谢您的帮助。
谢谢,但它不工作,因为在我的字符串的每一边有2方括号,我需要摆脱一对我认为但我不知道如何 – Frekell
方括号?给我们看一看。我认为这是一个编码问题 – thepaulo
[“[\”pw1 \“,\”pw2 \“,\”pw3 \“]”]这就是我的意思:) – Frekell