How to parse JSON using GSON?

允我心安 提交于 2019-12-13 06:10:18

问题


How get value in array "rate" to invoke getters methods ?

My Json response is something as below and confused how to parse it using GSON. Please have look on the following and guide me how i can parse it using GSON.

data.json

{  
 "query":{  
   "count":2,
   "created":"2017-01-03T12:45:19Z",
   "lang":"en-us",
   "results":{  

      "rate":[  
        {  
           "id":"BTC/USD",
           "Name":"BTCUSD",
           "Rate":"985.50",
           "Date":"1/3/2017",
           "Time":"10:35am",
           "Ask":"985.50",
           "Bid":"985.35"
        },
        {  
           "id":"BTC/EUR",
           "Name":"BTCEUR",
           "Rate":"973.16",
           "Date":"1/3/2017",
           "Time":"10:35am",
           "Ask":"973.16",
           "Bid":"973.10"
        }
     ]
  }
 }
}

I use classes to parse apart

Market.java

public class Market {

  @SerializedName("query")
  private Query query;

 public Query getQuery() {
  return query;
 }

 public void setQuery(Query query) {
  this.query = query;
 }

}

Query.java

 public class Query {

  @SerializedName("count")
  private Integer count;
  @SerializedName("created")
  private String created;
  @SerializedName("lang")
  private String lang;
  @SerializedName("results")
  private Results results;

  public Integer getCount() {
     return count;
  }

  public void setCount(Integer count) {
      this.count = count;
  }

  public String getCreated() {
     return created;
  }

  public void setCreated(String created) {
      this.created = created;
  }

  public String getLang() {
    return lang;
  }

  public void setLang(String lang) {
    this.lang = lang;
  }

  public Results getResults() {
    return results;
  }

  public void setResults(Results results) {
    this.results = results;
  }

 }

Rate.java

public class Rate {

@SerializedName("id")
private String id;
@SerializedName("Name")
private String name;
@SerializedName("Rate")
private String rate;
@SerializedName("Date")
private String date;
@SerializedName("Time")
private String time;
@SerializedName("Ask")
private String ask;
@SerializedName("Bid")
private String bid;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getRate() {
return rate;
}

public void setRate(String rate) {
this.rate = rate;
}

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

public String getTime() {
return time;
}

public void setTime(String time) {
this.time = time;
}

public String getAsk() {
return ask;
}

public void setAsk(String ask) {
this.ask = ask;
}

public String getBid() {
return bid;
}

public void setBid(String bid) {
this.bid = bid;
}

}

Results.java

public class Results {

@SerializedName("rate")
private List<Rate> rate = null;

public List<Rate> getRate() {
return rate;
}

public void setRate(List<Rate> rate) {
this.rate = rate;
}

}

Trying to get the values Main.java

 public class Main {

  /* ..... */
     Gson gson = new Gson();
    Market market = gson.fromJson(json, Market.class);
     //error: incompatible types: Rate cannot be converted to List<Rate>
    for( List<Rate> res : market.getQuery().getResults().getRate());
    {
        Log.v(LOG_TAG, res); // error: cannot find symbol variable res

    }

 }

how to do it properly ?


回答1:


The syntax of your for each loop is incorrect. Since you are attempting to loop through a list of Rate objects, the type of res should be a Rate, not a list of Rates:

for (Rate res : market.getQuery().getResults().getRate()) {
    // code here
}

As an aside, you should consider checking for null values before dereferencing all of those child objects as you run the risk of throwing a NullPointerException at runtime.



来源:https://stackoverflow.com/questions/41465318/how-to-parse-json-using-gson

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!