java - Convert multiple json results to iterable -
i trying map json results iterable of class instance.
if 1 object result convert json class instance using methods:
private objectreader personreader = null; private objectreader getpersonreader() { if (personreader != null) return personreader; objectmapper mapper = new objectmapper(); return personreader = mapper.reader(person.class); } public person response2person(responseentity<string> response) { try { return getpersonreader().readvalue(response.getbody()); } catch (ioexception e) { throw new runtimeexception(e); } }
what shoul getpersonsreader method convert json string, containing multiple results?
public iterable<person> response2persons(responseentity<string> response) { try { return getpersonsreader().readvalue(response.getbody()); } catch (ioexception e) { throw new runtimeexception(e); } }
the json string looks this:
[ { "firstname": "foo", "lastname": "bar" }, { "firstname": "elvis", "lastname": "presley" } ]
you can use typereference
make compile-time reference parameterised type:
mapper.reader(new typereference<list<person>>() {});
Comments
Post a Comment