I have JSON data like that:
[ {
"data": "data",
"what": "what2",
"when": 1392046352,
"id": 13,
"tags": "checkid:testcheckid, target:server.id.test, id:testid"
}, {
"data": "Test",
"what": "Test",
"when": 1395350977,
"id": 5,
"tags": "checkid:testcheckid, target:server.id.test, id:testid"
} ]
I have a Java class and I want to convert this JSON to a list of instances of my class. I found how to convert a single object, but not a list.
With Jackson you would do something like:
String jsonStr = "...";
ObjectReader reader = new ObjectMapper().reader();
List<Map<String, Object>> objs = reader.readValue(jsonStr);
Map<String, Object> myFirstObj = objs.get(0);
myFirstObj.get("data");
myFirstObj.get("what");
// and so on
Or even better you can create a pojo and do:
String jsonStr = "...";
ObjectMapper mapper = new ObjectMapper();
List<MyPojo> myObjs = mapper.readValue(jsonStr, new TypeReference<List<MyPojo>>() {});
MyPojo myPojo = myObjs.get(0);
myPojo.getData();
myPojo.getWhat();
// and so on
There are several libraries out there that will parse JSON, such as GSon and Jackson. If the functionality isn't provided out-of-box, you can write a custom deserializer that will do what you need.
I don't really understand your problem, if you already have and object just create a list (ArrayList for example) and add them.
ArrayList<YourObject> list = new ArrayList<YourObject>();
list.add(yourObject);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.