簡體   English   中英

Java用GSON解析JSON

[英]Java Parsing JSON with GSON

我是Java和JSON的新手,我正在嘗試使用GSON解析以下JSON。 但是,我遇到的一個問題是我沒有任何錯誤,但是對象只是空的。

{
    "pac": [
        {
            "customerName": "TEST"
        }
    ]
}

這個類是我試圖做的對象:

public class customer{

/** The customer name. */
private String customerName;

/**
 * Gets the customer name.
 *
 * @return the customer name
 */
public String getCustomerName() {
    return customerName;
}

/**
 * Sets the customer name.
 *
 * @param customerName the new customer name
 */
public void setCustomerName(String customerName) {
    this.customerName = customerName;
}

我正在用它來嘗試解析:

Gson gson = new Gson();
customer i = gson.fromJson(jsonFile, customer.class);

如果你們有任何建議,我將向您推薦。

您的JSON顯示有一個對象具有pac屬性。

此屬性pac是一組客戶

因此,您可以嘗試:

public class Customers {

    public List<customer> pac; // List from java.util

}

接着

Customers i = gson.fromJson(jsonFile, Customers.class);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM