java json objects best practice
A web api responses with this JSON:
// GET /sales/products+dates?start_date=2010-11-24end_date=2010-11-31
{
"12345": {
"2010-11-24": {
"date": "2010-11-24",
"country": null,
"iso": null,
"product_id": 12345,
"downloads": 11,
"net_downloads": 11,
"updates": 0,
"revenue": "0.00",
"returns": 0,
"gift_redemptions": 0,
"promos": 0
},
...
},
"123456": {
"2010-11-24": {
"date": "2010-11-24",
"product_id": 123456,
"downloads": 28,
"net_downloads": 29,
"updates": 6,
"revenue": "19.02",
"returns": 1,
"gift_redemptions": 0,
"promos": 0
},
...
}
}
With GSON in Java I want to get objects, which represent this JSON. So I
define a class like:
public class SalesList{
private ArrayList<SaleData> mySales;
private class SaleData{
private int downloads;
private int net_downloads;
private int updates;
private int returns;
private int gifts;
private int gift_redemptions;
private int promos;
private float revenue;
private String date;
private String product_id;
private String iso;
private String country;
private String product;
However this seems not to be realy the way to go, is it? Since the
response is a JSON Object, not Array, I'm not sure how to map the product
IDs (first level in JSON response), which can be 0 - infinite and the
dates (second level). Is arrays the right way? Or is it totaly wrong
approach to JSON in Java?
No comments:
Post a Comment