我想解析我的Android應用程序上的GSON庫的JSON。我能正確解析JSON陣列,但現在我需要解析另一個JSON,這個estructure:試圖解析與GSON的JSON
{
"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;
}
}
而一個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;
}
}
而且,在我的主活動我試圖得到所有的信息whith:
final Gson gson = new Gson();
Order o = gson.fromJson(jsonData, Order.class);
System.out.println(" - " + o.getOrderId());
工作正常,但如果我想獲得Articles info,va lues是空的,我不知道我怎麼能得到它。
我嘗試用類似的東西:
Type collectionType = new TypeToken<List<Merchants>>() {
}.getType();
data = gson.fromJson(response, collectionType);
我嘗試使用ArticlesWrapper,但我不知道如何做到這一點沒錯。但我想我忘記了一些東西......而我不知道是什麼。 在應用程序的另一部分,我正確反序列化JSON,因爲它是一個數組,但在這種情況下,我不知道如何正確地獲取所有數據。
如果您需要更多信息,我會爲您提供! 謝謝!
嗨亞歷克斯,非常感謝您的回答。我沒有足夠的時間來嘗試,但如果作品,我會給你我的整個聲譽!再次感謝。 – Pipeline
亞歷克斯只是勇敢地做你的工作:P – shkschneider