没有为类org.json.JSONObject找到序列化程序,也没有发现创建BeanSerializer的属性 [英] No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer
问题描述
从网络服务获取JSON,Json数组作为回复
having JSON from web service, Json Array as a response
[3]
0: {
id: 2
name: "a561137"
password: "test"
firstName: "abhishek"
lastName: "ringsia"
organization: "bbb"
}-
1: {
id: 3
name: "a561023"
password: "hello"
firstName: "hello"
lastName: "hello"
organization: "hello"
}-
2: {
id: 4
name: "a541234"
password: "hello"
firstName: "hello"
lastName: "hello"
organization: "hello"
}
在JsonArray中获取响应后在读取Json数组的Json对象时出错:
After Getting Response in JsonArray Getting error while reading Json Object of Json Array :
List<User> list = new ArrayList<User>();
JSONArray jsonArr = new JSONArray(response);
for (int i = 0; i < jsonArr.length(); i++) {
JSONObject jsonObj = jsonArr.getJSONObject(i);
ObjectMapper mapper = new ObjectMapper();
User usr= mapper.convertValue(jsonObj, User.class);
list.add(usr);
}
没有为类org.json找到序列化程序。 JSONObject并没有发现创建BeanSerializer的属性(为了避免异常,禁用SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS))
No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) )
推荐答案
首先要接受它作为Json数组,然后在读取它的Object时必须使用Object Mapper.readValue,因为Json Object仍然在String中。
have To Accept it first as a Json Array ,then while reading its Object have to use Object Mapper.readValue ,because Json Object Still in String .
List<User> list = new ArrayList<User>();
JSONArray jsonArr = new JSONArray(response);
for (int i = 0; i < jsonArr.length(); i++) {
JSONObject jsonObj = jsonArr.getJSONObject(i);
ObjectMapper mapper = new ObjectMapper();
User usr = mapper.readValue(jsonObj.toString(), User.class);
list.add(usr);
}
这篇关于没有为类org.json.JSONObject找到序列化程序,也没有发现创建BeanSerializer的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!