I’m trying to parse a JSON with GSON library on my Android App. I could parse correctly a JSON Array, but now I need to parse another json, with this estructure:
{
"articles": [
{
"article": {
"articleId": 1,
"articleName": "Bocadillo de calamares",
"merchantId": 2,
"price": 3.5
},
"computable": true,
"orderArticleId": 3157,
"orderId": 203,
"price": 3.5,
"requestedDate": "2012-11-19 13:15:20",
"shared": true,
"status": "AS01_INITIAL"
},
{
"article": {
"articleId": 3,
"articleName": "Desayuno",
"merchantId": 2,
"price": 2.2
},
"computable": true,
"orderArticleId": 3158,
"orderId": 203,
"price": 0,
"requestedDate": "2012-11-19 13:17:30",
"shared": true,
"status": "AS01_INITIAL"
},
{
"article": {
"articleId": 2,
"articleName": "Café",
"merchantId": 2,
"price": 1.1
},
"computable": true,
"orderArticleId": 3156,
"orderId": 203,
"price": 1.1,
"requestedDate": "2012-11-19 13:15:20",
"shared": true,
"status": "AS01_INITIAL"
},
{
"article": {
"articleId": 1,
"articleName": "Bocadillo de calamares",
"merchantId": 2,
"price": 3.5
},
"computable": true,
"orderArticleId": 3155,
"orderId": 203,
"price": 3.5,
"requestedDate": "2012-11-19 12:40:17",
"shared": true,
"status": "AS01_INITIAL"
}
],
"comment": null,
"creationDate": "2012-11-19 12:06:41",
"customer": {
"creationDate": 1353321506000,
"customerId": 152,
"devices": [
{
"customerId": 152,
"deviceId": "2000",
"phone": null
}
],
"image": false,
"mail": null,
"name": null,
"password": null
},
"finishDate": null,
"group": 0,
"groupOrders": [],
"location": {
"location": "Table 1",
"locationId": 1,
"shopId": 2
},
"orderId": 203,
"parentOrderId": 192,
"score": null,
"shopId": 2,
"status": "OS01_INITIAL",
"ticketUrl": null,
"tip": null,
"total": 0,
"totalArticles": 0,
"type": "BILL"
}
I have a Order.class like this:
public class Order {
private final String orderId;
(....)
private final ArrayList<Articles> articles;
private final String group;
public Order() {
orderId = "";
(....)
articles = new ArrayList<Articles>();
group = "";
}
public String getOrderId() {
return orderId;
}
(... All getters for each element)
public ArrayList<Articles> getArticles() {
return articles;
}
}
And a Articles.class
public class Articles {
private final String orderArticleId;
(...)
private final ArrayList<Article> article;
public Articles() {
orderArticleId = "";
(....)
article = new ArrayList<Article>();
};
public String getPrice() {
return price;
}
(....All getters for each element)
public ArrayList<Article> getArticle() {
return article;
}
}
And, on my main activity I try to get all the info whith this:
final Gson gson = new Gson();
Order o = gson.fromJson(jsonData, Order.class);
System.out.println(" - " + o.getOrderId() );
And works ok, but if I want to get Articles info, the values are null, and I don’t know how can I get it.
I try with something like that:
Type collectionType = new TypeToken<List<Merchants>>() {
}.getType();
data = gson.fromJson(response, collectionType);
And I try to use a ArticlesWrapper, but I don’t know how to do it exactly.
But I think I forgetting something… and I don’t know what.
In another part of the app, I deserialize correctly a JSON because it is an Array, but on this case I don’t know how can I get all the data correctly.
If you need more information, I will provide you!.
Thanks!
This is because you are not translating correctly the JSON structure to Java POJOs, basically omitting some fields you have the following structure.
So you should end up with 6 Java POJOs that GSon should be able to populate while deserializing the JSON data.
I just translated your JSON structure to POJO here, and I’ve let Dates as String to simplify the parsing. If you want to have
java.util.Datepopulated correctly you will have to customize the GSon instance.Main class
Order
BigArticle
Article
Customer
Device
Location
Output