Java JSON deserialize String with property names as field names -
i receiving api json that:
{ "channel":"masta", "starttime":1427673600000, "endtime":1427760000000, "totaluniques":1, "totalviewtime":1927, "totalviews":13, "totalcountries":1, "countries":{ "us":{ "uniques":1, "views":13, "viewtime":1927 } } }
now want deserialize class, class(stats) have fields channel, starttime , on. how handle countries property?
i thought making class countries not sure cause it's have "us" property name. not "country": "us". , what's more has own parameters. how deserialize it?
mostly using objectmapper object.readvalue(jsonstring) don't know how handle 'countries'. in example 1 country 'us' can more.
declare country
class:
public class country { private int uniques; private int views; private int viewtime; public int getuniques() { return uniques; } public void setuniques(int uniques) { this.uniques = uniques; } public int getviews() { return views; } public void setviews(int views) { this.views = views; } public int getviewtime() { return viewtime; } public void setviewtime(int viewtime) { this.viewtime = viewtime; } }
in stats
class should declare countries
map of country
objects:
public class stats { private string channel; private long starttime; private long endtime; private int totaluniques; private int totalviewtime; private int totalviews; private int totalcountries; ... private map<string, country> countries; public map<string, country> getcountries() { return countries; } public void setcountries(map<string, country> countries) { this.countries = countries; } }
now can deserialize object:
objectmapper mapper = new objectmapper(); stats stats = mapper.readvalue(jsonstring, stats.class);
after deserialization stack
object map 1 country
object key "us".
Comments
Post a Comment