gpt4 book ai didi

java - 尝试使用 GSON 解析 JSON

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:20:23 28 4
gpt4 key购买 nike

我正在尝试在我的 Android 应用程序上使用 GSON 库解析 JSON。我可以正确解析一个 JSON 数组,但现在我需要用这个结构解析另一个 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;
}

并且,在我的主要 Activity 中,我尝试获取所有信息:

    final Gson gson = new Gson();
Order o = gson.fromJson(jsonData, Order.class);
System.out.println(" - " + o.getOrderId() );

工作正常,但如果我想获取文章信息,值是空的,我不知道如何获取它。

我尝试类似的东西:

 Type collectionType = new TypeToken<List<Merchants>>() {
}.getType();
data = gson.fromJson(response, collectionType);

我尝试使用 ArticlesWrapper,但我不知 Prop 体如何操作。但我想我忘记了什么……我不知道是什么。在应用程序的另一部分,我正确反序列化了一个 JSON,因为它是一个数组,但在这种情况下,我不知道如何才能正确获取所有数据。

如果您需要更多信息,我会为您提供!。谢谢!

最佳答案

这是因为您没有将 JSON 结构正确地转换为 Java POJO,基本上省略了您具有以下结构的一些字段。

+ Order
+ List<BigArticle> articles
+ Customer customer
+ Location location
+ other basic fields

+ BigArticle
+ Article article
+ other basic fields

+ Article
+ basic fields

+ Customer
+ List<Device> devices
+ other basic fields

+ Device
+ basic fields

+ Location
+ basic fields

因此,您最终应该得到 6 个 Java POJO,GSon 应该能够在反序列化 JSON 数据时填充这些 Java POJO。

我刚刚在这里将您的 JSON 结构转换为 POJO,并且我将 Dates 设置为 String 以简化解析。如果您想正确填充 java.util.Date,则必须自定义 GSon 实例。

主类

public class Main {
public static void main(String[] args) {
String jsonData = "...";
final Gson gson = new Gson();
Order o = gson.fromJson(jsonData, Order.class);
System.out.println("orderId: " + o.getOrderId());
System.out.println("first article price: " + o.getArticles().get(0).getPrice());
System.out.println("second article status: " + o.getArticles().get(1).getStatus());
System.out.println("third article requested date: " + o.getArticles().get(2).getRequestedDate());
}
}

顺序

public class Order {
private List<BigArticle> articles;
private String comment;
private String creationDate;
private Customer customer;
private String finishDate;
private int group;
private List<Integer> groupOrders;
private Location location;
private int orderId;
private int parentOrderId;
private int score;
private int shopId;
private String status;
private String ticketUrl;
private String tip;
private int total;
private int totalArticles;
private String type;

// getters and setters
}

大文章

public class BigArticle {
private Article article;
private boolean computable;
private int orderArticleId;
private int orderId;
private double price;
private String requestedDate;
private boolean shared;
private String status;

// getters and setters
}

文章

public class Article {
private int articleId;
private String articleName;
private int merchantId;
private double price;

// getters and setters
}

客户

public class Customer {
private long creationDate;
private int customerId;
private List<Device> devices;
private boolean image;
private String mail;
private String name;
private String password;

// getters and setters
}

设备

public class Device {
private int customerId;
private String deviceId;
private String phone;

// getters and setters
}

位置

public class Location {
private String location;
private int locationId;
private int shopId;

// getters and setters
}

输出

orderId: 203
first article price: 3.5
second article status: AS01_INITIAL
third article requested date: 2012-11-19 13:15:20

关于java - 尝试使用 GSON 解析 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13463276/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com