简体   繁体   中英

Using Retrofit in nested JSON Object

I am playing with Retrofit. I have trouble when the json value is/are JSONObject s like the ratings key

[{
    "title": "True Blood",
    "year": 2008,
    "watchers": 36,
    "ratings": {
        "percentage": 82,
        "votes": 8377,
        "loved": 7629,
        "hated": 748
    }
  }
]

In jsonschema2pojo , it says I have to create ratings key into another class

static class Show {
    String title;
    int year;
    Ratings ratings;
}

static class Ratings {
    int percentage;
}

interface TrendingService {
    @GET("/shows/trending.json/{yourApiKey}")
    public List<Show> getTrendingShows(@Path("yourApiKey") String yourApiKey);
}

public void onCreate(Bundle savedInstanceState) {
    RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint(API_URL)
            .build();

    final TrendingService service = restAdapter.create(TrendingService.class);

    new AsyncTask<Void, Void, List<Show>>() {
        @Override
        protected List<Show> doInBackground(Void... params) {
            return service.getTrendingShows("ApiKey");
        }

        protected void onPostExecute(List<Show> shows) {
            super.onPostExecute(shows);
            for (Show show : shows) {
                Log.d("Show", String.format(
                        "%s %s %d", show.title, show.poster, show.ratings.percentage));
                        // NULL
            }
        }
    }
}

but the show.ratings.percentage is always null . Is there something I miss here?

Retrofit uses Gson by default so this should fix your problem.

import com.google.gson.annotations.Expose;

public class Show {

@Expose
private String title;
@Expose
private Integer year;
@Expose
private Integer watchers;
@Expose
private Ratings ratings;

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public Integer getYear() {
    return year;
}

public void setYear(Integer year) {
    this.year = year;
}

public Integer getWatchers() {
    return watchers;
}

public void setWatchers(Integer watchers) {
    this.watchers = watchers;
}

public Ratings getRatings() {
    return ratings;
}

public void setRatings(Ratings ratings) {
    this.ratings = ratings;
}

}

And the Ratings class

import com.google.gson.annotations.Expose;

public class Ratings {

@Expose
private Integer percentage;
@Expose
private Integer votes;
@Expose
private Integer loved;
@Expose
private Integer hated;

public Integer getPercentage() {
    return percentage;
}

public void setPercentage(Integer percentage) {
    this.percentage = percentage;
}

public Integer getVotes() {
    return votes;
}

public void setVotes(Integer votes) {
    this.votes = votes;
}

public Integer getLoved() {
    return loved;
}

public void setLoved(Integer loved) {
    this.loved = loved;
}

public Integer getHated() {
    return hated;
}

public void setHated(Integer hated) {
    this.hated = hated;
}

}

Let me know if that helped.

How about using with annotations, like below:

@JsonPropertyOrder({
"title",
"year",
"watchers",
"ratings"
})
public class Example {

@JsonProperty("title")
private String title;
@JsonProperty("year")
private Integer year;
@JsonProperty("watchers")
private Integer watchers;
@JsonProperty("ratings")
private Ratings ratings;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("title")
public String getTitle() {
return title;
}

@JsonProperty("title")
public void setTitle(String title) {
this.title = title;
}

@JsonProperty("year")
public Integer getYear() {
return year;
}

@JsonProperty("year")
public void setYear(Integer year) {
this.year = year;
}

@JsonProperty("watchers")
public Integer getWatchers() {
return watchers;
}

@JsonProperty("watchers")
public void setWatchers(Integer watchers) {
this.watchers = watchers;
}

@JsonProperty("ratings")
public Ratings getRatings() {
return ratings;
}

@JsonProperty("ratings")
public void setRatings(Ratings ratings) {
this.ratings = ratings;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.example.Ratings.java-----------------------------------

package com.example;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
"percentage",
"votes",
"loved",
"hated"
})
public class Ratings {

@JsonProperty("percentage")
private Integer percentage;
@JsonProperty("votes")
private Integer votes;
@JsonProperty("loved")
private Integer loved;
@JsonProperty("hated")
private Integer hated;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("percentage")
public Integer getPercentage() {
return percentage;
}

@JsonProperty("percentage")
public void setPercentage(Integer percentage) {
this.percentage = percentage;
}

@JsonProperty("votes")
public Integer getVotes() {
return votes;
}

@JsonProperty("votes")
public void setVotes(Integer votes) {
this.votes = votes;
}

@JsonProperty("loved")
public Integer getLoved() {
return loved;
}

@JsonProperty("loved")
public void setLoved(Integer loved) {
this.loved = loved;
}

@JsonProperty("hated")
public Integer getHated() {
return hated;
}

@JsonProperty("hated")
public void setHated(Integer hated) {
this.hated = hated;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM