如何使用GSON解析JSON?

时间:2017-01-04 13:44:45

标签: java json gson android

如何获取数组“rate”中的值以调用getter方法?

我的Json响应如下所示,并且混淆了如何使用GSON解析它。请查看以下内容并指导我如何使用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"
        }
     ]
  }
 }
}

我使用类来解析

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

}

尝试获取值 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

    }

 }

如何正确地做到这一点?

1 个答案:

答案 0 :(得分:1)

每个循环的语法都不正确。由于您试图遍历Rate对象列表,res的类型应该是Rate,而不是Rate列表:

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

顺便说一下,在解除引用所有这些子对象之前,应该考虑检查空值,因为你冒着在运行时抛出NullPointerException的风险。