Khayam Gondal Khayam Gondal - 1 year ago 169
Java Question

Mapping JSON with Map and Normal Objects

I am getting following JSON

{
'routers':
{
'external_gateway_info':{ },
'description':'',
'gw_port_id':'1f290c84-02fe-4bdb-a063-77a425cc10f5',
'tenant_name':'lmco-test'
},
'tenants':{
'82d1df3fcfc14ac594c6f5c5b6dcad6a':'lmco-test',
'6eb4b2dd40dc4d63a3c8a691c371dd06':'admin',
'10b0726823784ceca219821d44a2b5d1':'ivs-perf',
'VRRP_Service':'VRRP_Service',
'8fb76703c3084308a01d113751560730':'service'
}
}


Now routers is a simple object where I am mapping keys/values to my class Routers.java but tenants are a HashMap.
I am unable to find a way where I can use a single mapper to map both routers and tenants to a single Object

My mapper

JSONObject jsonData = new JsonRepresentation(entity).getJsonObject();
RootModel rootModel = mapper.readValue(jsonData.toString(), RootModel.class);


Where Root.java snippet

public class RootModel {

private Router routers;
private Tenant tenants;


public RootModel(@JsonP
...
...
}

Answer Source

Apparently there is no cleaner way to use one Map for this kind of data. What I did, I used two different mappers to map routers and tenants.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download