2015-09-06 43 views
1

我使用這個JSON存儲與改造的一些文章,但我有一個錯誤:爲什麼Gson期望開始數組,但獲得一個對象?

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2

這裏是我的代碼:

RestAdapter restAdapter = new RestAdapter.Builder() 
     .setEndpoint("http://www.example.com/") 
     .setLogLevel(RestAdapter.LogLevel.FULL) 
     .build(); 

Flow flowservice = restAdapter.create(Flow.class); 

flowservice.getArticles("55ec320b066ee7ae08360f12", new Callback<List<String>>() { 

    @Override 
    public void success(List<String> objects, Response response) { 
     textView.setText(objects.get(0)); 
    } 

    @Override 
    public void failure(RetrofitError error) { 
     textView.setText(error.getMessage()); 
    } 
}); 

而且我用這個POJO:

public class Article implements Serializable { 
    private int remoteId; 
    private String title; 
    private String imageUrl; 
    private String content; 
    private int viewsCount; 
    private int commentsCount; 
    private int likesCount; 
    private int categoryId; 
    private String authorName; 
    private boolean liked; 

    // getters and setters removed 
} 

和一個樣本JSON:

{ 
    "success":true, 
    "errorCode":0, 
    "articles":[ 
     { 
     "remoteId":0, 
     "title":"Nam viverra vulputate lacus nec pellentesque. Nam viverra vulputate lacus nec pellentesque.", 
     "imageUrl":"/articles/armatis.png", 
     "content":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas interdum tempus ultrices. Ut quis tellus molestie, ornare mi non, fermentum nisl. Sed vitae ultricies metus. Vivamus aliquam metus enim, ut fringilla justo molestie et. Maecenas nec ligula neque. Mauris vel cursus risus. Suspendisse ut nulla porta, congue nulla sit amet.", 
     "viewsCount":235, 
     "commentsCount":6, 
     "likesCount":18, 
     "categoryId":1, 
     "authorName":"Sébastien Gabory", 
     "liked":false 
     }, 
     { 
     "remoteId":1, 
     "title":"Nam viverra vulputate lacus nec pellentesque.", 
     "imageUrl":"/articles/armatis.png", 
     "content":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas interdum tempus ultrices. Ut quis tellus molestie, ornare mi non, fermentum nisl. Sed vitae ultricies metus. Vivamus aliquam metus enim, ut fringilla justo molestie et. Maecenas nec ligula neque. Mauris vel cursus risus. Suspendisse ut nulla porta, congue nulla sit amet.", 
     "viewsCount":18, 
     "commentsCount":6, 
     "likesCount":25, 
     "categoryId":1, 
     "authorName":"Sébastien Gabory", 
     "liked":false 
     } 
    ] 
} 

流量類:

public interface Flow { 
    @GET("/v2/{json}") 
    void getArticles (@Path("json") String json ,Callback<List<Article>> callback); 
} 

回答

3

GSON試圖您的JSON響應直接轉換成的文章列表。但是,您的文章列表實際上嵌套在外部JSON對象內一層深處。當GSON嘗試此外對象轉換成JSON數組(最終轉化爲Java List),因此它拋出,你看到的錯誤:

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2

一個解決將是使一個新的POJO代表整個JSON響應。例如:

public class ArticleListWrapper implements Serializable { 
    private boolean success; 
    private double errorCode; 
    private List<Article> articles; 

    // getters and setters, etc. 
} 

然後你Flow類將被更新如下:然後

@GET("/v2/{json}") 
void getArticleListWrapper (@Path("json") String json, Callback<ArticleListWrapper> callback); 

GSON應該能夠正確地反序列化JSON響應到ArticleListWrapper對象。您可以通過訪問此ArticleListWrapper對象的articles字段來檢索回調中的文章列表。

相關問題