从RetroFit库返回LinkedTreeMap对象的问题

时间:2015-08-25 19:02:38

标签: android retrofit treemap

当我尝试使用reviewdata.result.get("结果")访问结果时,它需要一个对象并且不会让我遍历结果=(!我正在寻找获得结果的方法将节点转换为我可以使用的结构。感谢任何帮助。

0 = {LinkedTreeMap$Node@3888} "page" -> "1.0"
1 = {LinkedTreeMap$Node@3889} "results" -> " size = 2"
2 = {LinkedTreeMap$Node@3890} "total_pages" -> "1.0"
3 = {LinkedTreeMap$Node@3891} "total_results" -> "2.0"  

2 个答案:

答案 0 :(得分:0)

如果在尝试使用gson Linked Tree Maps = D时遇到其他人遇到此问题,这是完整的代码!

    LinkedTreeMap treemap = reviewdata.result;
    ArrayList<LinkedTreeMap> subtree = (ArrayList<LinkedTreeMap>) treemap.get("results");
    ArrayList<Review> reviews = new ArrayList<>();

    for(LinkedTreeMap map : subtree){
        Review review = new Review();
        review.setReviewID(map.get("id").toString());
        review.setAuthor(map.get("author").toString());
        review.setContent(map.get("content").toString());
        review.setURL(map.get("url").toString());
        reviews.add(review);
    }

    return  reviews;

答案 1 :(得分:0)

这个解决方法对我有用:

Class<M> modelClass;
final List<M> modelList = new ArrayList<>();

@Override
public void success(List<Object> resultList, Response response) {
    modelList.clear();
    for (Object result : resultList) {
        String json = new Gson().toJson(result);
        M model = new Gson().fromJson(json, modelClass);
        modelList.add(model);
    }
    Log.wtf(TAG, "GET Successful");
}