2014-01-13 154 views
-1

我想反序列化我的對象,但我不能得到任何錯誤!Json反序列化錯誤

我在android上使用gson來讀取json文件。

,這是我的請求類,我利用休息服務器我反序列化。

 public Object getListaSeguradoras(String _codPais, String _tipoPesquisa) 
{ 

    try { 

     HttpClient httpClient = new DefaultHttpClient(); 

     HttpPost post = new HttpPost(Config.WS_PATH); 
     post.setHeader("content-type", "application/json; charset=UTF-8"); 
     post.addHeader("Client-Application","xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); 


     //Make object in JSON format 
     JSONObject dato = new JSONObject(); 

     dato.put("CodPais", _codPais); 
     dato.put("TipoPesquisa", _tipoPesquisa); 

     StringEntity entity = new StringEntity(dato.toString()); 
     post.setEntity(entity); 

     HttpResponse resp = httpClient.execute(post); 
     String respStr = EntityUtils.toString(resp.getEntity()); 

     Gson gson = new Gson(); 

     Type collectionType = new TypeToken<Collection<SeguradoraLista>>(){}.getType(); 
     Collection<SeguradoraLista> ListaSeguradoras = gson.fromJson(respStr, collectionType); 

     return ListaSeguradoras; 

    } catch (Exception e) { 
     e.printStackTrace(); 
     return null; 
    } 
} 

我的目標:

這是我的getter和setter

public class SeguradoraLista{ 

    public String SeguradoraId; 
    public String Nome; 

    public String getSeguradoraId() { 
     return this.SeguradoraId; 
    } 

    public String getNome() { 
     return this.Nome; 
    } 

    public void setSeguradoraId (String SeguradoraId) { 
     this.SeguradoraId = SeguradoraId; 
    } 

    public void setNome (String Nome) { 
     this.Nome = Nome; 
    } 
} 

我的JSON:

這是對我的字符串JSON字符串respStr

 [{"SeguradoraId":"19","Nome":"AIG Europe"}, 
{"SeguradoraId":"25","Nome":"AIOI Insurance Co. Of Europe"}, 
{"SeguradoraId":"28","Nome":"Aktiv Forsikring A/S"}, 
{"SeguradoraId":"160","Nome":"Enter Forsikring AS"}, 
{"SeguradoraId":"167","Nome":"Euro Insurances Ltd."}, 
{"SeguradoraId":"189","Nome":"Försäkrings-AB Volvia"}, 
{"SeguradoraId":"219","Nome":"Gjensidige NOR Forsikring"}, 
{"SeguradoraId":"245","Nome":"If Skadeforsikring NUF"}, 
{"SeguradoraId":"265","Nome":"Jernbanepersonalets Forsikring Gjensiding"}, 
{"SeguradoraId":"271","Nome":"KLP Skadeforsikring AS"}, 
{"SeguradoraId":"284","Nome":"Landbruksforsikring"}, 
{"SeguradoraId":"309","Nome":"Lloyd's v/Vital Skade AS"}, 
{"SeguradoraId":"459","Nome":"SpareBank 1 Skadeforsikring AS"}, 
{"SeguradoraId":"472","Nome":"Tennant Forsikring nuf"}, 
{"SeguradoraId":"473","Nome":"Terra Skadeforsikring AS"}, 
{"SeguradoraId":"494","Nome":"Trygg-Hansa Forsikring Nuf"}, 
{"SeguradoraId":"517","Nome":"Vesta Skadeforsikring A/S"}, 
{"SeguradoraId":"536","Nome":"Zürich Forsikring"}] 

我做錯了什麼?我不能使這個直接去實現?

+0

什麼是'SeguradoraLista'? –

+0

對不起,是我的對象,我修復了我的帖子。 – kaub0st3r

回答

1

您似乎有一個SeguradoraLista對象的數組。

所以只需創建類型的TypeTokenList<SeguradoraLista>

Type collectionType = new TypeToken<List<SeguradoraLista>>() {}.getType(); 
List<SeguradoraLista> ListaSeguradoras = gson.fromJson(respStr, collectionType); 

事實上,Collection<SeguradoraLista>將正常工作。

+0

對不起我修復我的帖子,SeguradoraLista是我的對象 – kaub0st3r

+0

我嘗試使用列表而不工作!暫時我使用這個(THOR):'ArrayList roomTypes = new ArrayList (); JSONArray jsonArray = new JSONArray(respStr); (int i = 0; i kaub0st3r

+0

@ kaub0st3r如果您從HTTP響應中獲得的JSON是您向我們展示的內容,那麼我上面的內容將工作。其實,你在你的問題中應該也能很好地工作。 –