GSON在JSON解析中的问题

时间:2013-03-11 10:21:21

标签: json performance gson

我在GSON的JSON解析中遇到问题

我的JSONERESPONSE是

    {"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();

注意:我无法更改响应,因此不建议

谢谢

2 个答案:

答案 0 :(得分:1)

Services.java

public class Services {

    private List<Service> services;

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

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

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;
    }
}

你的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数组。所以它暗示你需要两个Class,一个代表json数组中的每个项目 - Service.java,另一个代表一个包含项目列表的包装器。

答案 1 :(得分:1)

好的,我们需要创建一个中间层类来使其正确。 的 Services.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;
    }
}

<强> ServiceWrapper.java

public class ServiceWrapper {
    private Service service;
    public Service getService() {
        return service;
    }
    public void setService(Service service) {
        this.service = 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());
}