2013-03-11 47 views
0

JSON解析我通過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(); 

注:我不能改變的響應,所以不建議它

謝謝

+0

@ T.J.Crowder我編輯過的問題謝謝 – Android 2013-03-11 10:27:36

+0

@ test:引用您迄今嘗試過的代碼,以幫助人們幫助您。 – 2013-03-11 10:29:13

回答

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()); 
} 
+0

謝謝,它現在正在工作 – Android 2013-03-12 04:40:04

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數組。所以它意味着你需要兩個類,一個表示json數組內的每個項目--Service.java,另一個扮演包含項目列表的包裝器。

+0

@spiritwlker你是否檢查過getServiceList()的大小? – Android 2013-03-11 10:40:48

+0

它爲我打印3。你正在使用什麼版本的GSON? – spiritwalker 2013-03-11 10:42:38

+0

你是對的,謝謝,還有一個問題是我們可以使用ArrayList來代替列表 – Android 2013-03-11 10:50:48