java—将list< customclass>添加到jsonobject中,向jsonobject添加地址

xkrw2x1b  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(278)

h、 我试图将list添加到jsonobject(org.json.simple.jsonobject)中,当我打印jsonobject时,我看到地址被添加到jsonobject中。当我尝试用另一种方法解析jsonobject时,我得到了parse exception,这很自然,因为我的json对象看起来像这样:

{"firstKey":"key1",
 "secondKey":"secondKey",
"customKey":Ljava@23422}

如何将列表保存到jsonobject中:

{ "firstKey":"key1",
"customKey":[{"name":"Lucifer", "lastName":"Geralt"}]}

请帮忙提些有价值的建议

clj7thdc

clj7thdc1#

好吧,我能解决,请参考这个解决方案,如果你面临类似的问题:

JSONObject jsonobject= new JSONObject();
ObjectMapper mapper= new ObjectMapper();
List<User> user= repository.findAll();
String s= mapper.writeValueAsString(user);
eventMessageProperties.put("userList", s);
//to Parse
mapper.readValue((String)thingObject.get("userList"), new 
TypeReference<List<User>>(){});

相关问题