2016-12-28 71 views
0

我發現了很多這個異常的答案,但coudl'n找不到一個幫助我的情況。

我想使用GSON來解析JSON。這裏是我的代碼:

public <T> T getObject(String[] caminho, String[] parametros, Class<T> tipoRetorno) { 
     T resultado = null; 
     WebResource webResource = getWebResource(caminho, parametros); 
     ClientResponse response = getBuilder(webResource).get(ClientResponse.class); 
     if (response.getStatus() == Status.OK.getStatusCode()) { 
      JSONObject json = null; 
      try { 
       json = new JSONObject(response.getEntity(String.class)); 
      } catch (ClientHandlerException | UniformInterfaceException | JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      resultado = new Gson().fromJson(json.toString(), tipoRetorno); 
     } else if (response.getStatus() == Status.NO_CONTENT.getStatusCode()) { 
      String msg = "Getting cross-connections. URI: " + webResource.getURI() + " status: " + response.getStatus() 
        + " " + response.getStatusInfo(); 
      logger.info(msg); 
     } else { 
      String msg = "Error getting cross-connections. URI: " + webResource.getURI() + " returned error : " 
        + response.getStatus() + " status: " + response.getStatusInfo(); 
      logger.info(msg); 
     } 
     return resultado; 
    } 

這裏的JSON從 'JSON' 返回:

{"data":[{"thumbs":[{"id":79204454,"updated_at":"2016-12-24T19:54:48.000Z","created_at":"2016-12-24T19:54:48.000Z","filename":"54857bd6-0ccc-48d2-b5c8-8c4483954789_1482608998509","test_case_id":8172839,"url":"https://s3-eu-west-1.amazonaws.com/euthumbtestingbot/54857bd6-0ccc-48d2-b5c8-8c4483954789_1482608998509.jpg","custom":false},{"id":79204455,"updated_at":"2016-12-24T19:54:48.000Z","created_at":"2016-12-24T19:54:48.000Z","filename":"54857bd6-0ccc-48d2-b5c8-8c4483954789_1482609000499","test_case_id":8172839,"url":"https://s3-eu-west-1.amazonaws.com/euthumbtestingbot/54857bd6-0ccc-48d2-b5c8-8c4483954789_1482609000499.jpg","custom":false},{"id":79204456,"updated_at":"2016-12-24T19:54:48.000Z","created_at":"2016-12-24T19:54:48.000Z","filename":"54857bd6-0ccc-48d2-b5c8-8c4483954789_1482609001762","test_case_id":8172839,"url":"https://s3-eu-west-1.amazonaws.com/euthumbtestingbot/54857bd6-0ccc-48d2-b5c8-8c4483954789_1482609001762.jpg","custom":false}...} 

而 'tipoRetorno' 是類br.usp.icmc.testingbot.beans.AgrupamentoTestes:

package br.usp.icmc.testingbot.beans; 

import java.util.List; 

public class AgrupamentoTestes { 

    private List<Teste> data; 
    private Meta meta; 

    public List<Teste> getData() { 
     return data; 
    } 

    public void setData(List<Teste> data) { 
     this.data = data; 
    } 

    public Meta getMeta() { 
     return meta; 
    } 

    public void setMeta(Meta meta) { 
     this.meta = meta; 
    } 

} 

我得到這個異常:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 18101 path $.data[0].groups[0] 

爲什麼Gson不能正確地將我的JSON文本轉換爲我的POJO類型?

回答

1

如果將字符串粘貼到文本編輯器中,您將能夠在底部看到行號和列號,並找到錯誤所在的列18101。

+0

你好,丹,我用'......'的標誌不要太長。 – ricardoramos

+0

我得到了用於調試eclipse的JSON以及調試縮寫JSON以及 – ricardoramos

相關問題