2014-01-13 189 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是您向我们展示的内容,那么我上面的内容将工作。其实,你在你的问题中应该也能很好地工作。 –