繁体   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