Doug Ray
Doug Ray

Reputation: 1010

Issues with LinkedTreeMap object returned from RetroFit library

When i try to access the results with reviewdata.result.get("results") it expects an object and wont let me iterate through results =( ! I am looking for a way to get the resuluts node into a structure I can use. Any help is appreciated.

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"  

Upvotes: 2

Views: 1446

Answers (2)

LEMUEL  ADANE
LEMUEL ADANE

Reputation: 8826

This workaround worked for me:

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");
}

Upvotes: 1

Doug Ray
Doug Ray

Reputation: 1010

Here is the complete code if anyone else runs into this issue when trying to work with 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;

Upvotes: 0

Related Questions