0

我在 GSON 解析 JSON 时遇到问题

我的 JSONERSPONSE 是

    {"services":[{"service":{"name":"asd","id":"1"}},
    {"service":{"name":"asdf","id":"2"}},
    {"service":{"name":"asdfg","id":"3"}}]}

如何解析这个响应?

意味着我在创建上述响应类时遇到问题我已经创建了服务类,但我对如何创建服务类感到困惑。

public class services {

@SerializedName("service")
ArrayList<service> list;

public services(){
    System.out.println("services constructor stuff");
    list= new ArrayList<service>();
}

/**
 * @return the list
 */
public ArrayList<service> getList() {
    return list;
}

/**
 * @param list the list to set
 */
public void setList(ArrayList<service> list) {
    this.list = list;
}

}

but getting 0 in getList();

注意:我无法更改响应,所以不建议这样做

谢谢

4

2 回答 2

1

服务.java

public class Services {

    private List<Service> services;

    public List<Service> getServiceList() {
        return services;
    }

    public void setServiceList(List<Service> services) {
        this.services = services;
    }
}

服务.java

public class Service {

    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

你的 json 解析逻辑在这里:

Gson gson = new Gson();
String s = "{\"services\":[{\"service\":{\"name\":\"asd\",\"id\":\"1\"}},{\"service\":{\"name\":\"asdf\",\"id\":\"2\"}},{\"service\":{\"name\":\"asdfg\",\"id\":\"3\"}}]}";
Services services = gson.fromJson(s, Services.class);

基本上,json字符串并不是一个简单的json数组格式,它实际上是一个json对象里面的一个json数组。所以这意味着您需要两个类,一个代表 json 数组中的每个项目 - Service.java,另一个作为包含项目列表的包装器。

于 2013-03-11T10:34:48.150 回答
1

好的,我们需要创建一个中间层类来使它正确。 服务.java

public class Services {
    private ArrayList<ServiceWrapper> services = new ArrayList<ServiceWrapper>();
    public ArrayList<ServiceWrapper> getServices() {
        return services;
    }
    public void setServices(ArrayList<ServiceWrapper> services) {
        this.services = services;
    }
}

服务包装器.java

public class ServiceWrapper {
    private Service service;
    public Service getService() {
        return service;
    }
    public void setService(Service service) {
        this.service = service;
    }
}

服务.java

public class Service {
    private int id;
    private String name;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

以下是测试代码

Gson gson = new Gson();
String s = "{\"services\":[{\"service\":{\"name\":\"asd\",\"id\":\"1\"}},{\"service\":{\"name\":\"asdf\",\"id\":\"2\"}},{\"service\":{\"name\":\"asdfg\",\"id\":\"3\"}}]}";
Services services = gson.fromJson(s, Services.class);
for(ServiceWrapper serviceWrapper : services.getServices()){
    System.out.println(serviceWrapper.getService().getId());
    System.out.println(serviceWrapper.getService().getName());
}
于 2013-03-12T00:00:22.260 回答