spring - Jackson Json Object mapper : how to map an array? -
i trying map array backend api call. how can map data knowing :
the following classes used hold json array data :
@data private static class userlistbean { private list<userbean> userlist; } @data private static class userbean { private string id; private string username; private string password; }
the json have following format (the following example have 1 item in it) :
[ { "id":1, "username":"bob", "password":"403437d5c3f70b1329f37a9ecce02adbbf3e986" } ]
i using jackson , have tried following far keeps sending me exception
final objectmapper mapper = new objectmapper(); mapper.configure(feature.fail_on_unknown_properties, false); final objectreader reader = mapper.reader(userlistbean.class); geohubaccountlistbean accounts = null; try { accounts = reader.readvalue(jsonstring); } catch (final ioexception ex) { log.error("cannot convert json list of users", ex); }
here final objectreader reader = mapper.reader(userlistbean.class); throws exception
can not deserialize instance of com.xxx.xxx$userlistbean out of start_array token
any idea ?
thanks
well, if trying deserialize json object of type userlistbean
, need deserialize jsonobject (java objects tend map jsonobjects).
therefore, outer json construct should object. outer json construct jsonarray.
your userlistbean
has single field, list<userbean>
. top level json construct (which jsonobject) should contain single field name 'userlist' value jsonarray (java collections tend map jsonarrays).
i think actual json looking for:
{ "userlist":[ { "id":1, "username":"bob", "password":"403437d5c3f70b1329f37a9ecce02adbbf3e986" } ] }
if have no control on json coming in, want ditch parent object userlistbean
, deal directly list<userbean>
, work json have provided.
Comments
Post a Comment