我有两个类似结果的模态类,另一个是学生 我希望使用gson在两个模态类中获得所有json响应我尝试这种方式
* json回复: - {“成功”:“1”,“请求”:“/ api / v1 / getmarketsummaries”,“message”:“”,“结果”:[{“MarketID”:“18” “LastPrice”: “0.01458900”, “改变”: “ - 1.10”, “HighPrice”: “0.01497193”, “LowPrice”: “0.01453000”, “音量”: “5.27365150”, “BTCVolume”: “5.27365150” “TradeCount”: “344”, “BidPrice”: “0.01458012”, “AskPrice”: “0.01470990”, “BuyOrderCount”: “228”, “SellOrderCount”: “864”},{ “MarketID”: “43”, “LastPrice”: “0.00004443”, “改变”: “0.00”, “HighPrice”: “0.00000000”, “LowPrice”: “0.00000000”, “音量”: “0.00000000”, “BTCVolume”: “0.00000000”,“TradeCount “:”0“,”BidPrice“:”0.00003363“,”AskPrice“:”0.00004433“,”BuyOrderCount“:”29“,”SellOrderCount“:”432“}}} *
如何使用 Gson
在模态类中设置所有响应
Gson gson = new Gson();
Gson gson = new Gson();
结果result = gson.fromJson(response,Result.class);
但是无法获得那些数据,我怎样才能使它成为可能......
public class Result {
@SerializedName("MarketID")
@Expose
private String marketID;
@SerializedName("LastPrice")
@Expose
private String lastPrice;
@SerializedName("Change")
@Expose
private String change;
@SerializedName("HighPrice")
@Expose
private String highPrice;
@SerializedName("LowPrice")
@Expose
private String lowPrice;
@SerializedName("Volume")
@Expose
private String volume;
@SerializedName("BTCVolume")
@Expose
private String bTCVolume;
@SerializedName("TradeCount")
@Expose
private String tradeCount;
@SerializedName("BidPrice")
@Expose
private String bidPrice;
@SerializedName("AskPrice")
@Expose
private String askPrice;
@SerializedName("BuyOrderCount")
@Expose
private String buyOrderCount;
@SerializedName("SellOrderCount")
@Expose
private String sellOrderCount;
public String getMarketID() {
return marketID;
}
public void setMarketID(String marketID) {
this.marketID = marketID;
}
public String getLastPrice() {
return lastPrice;
}
public void setLastPrice(String lastPrice) {
this.lastPrice = lastPrice;
}
public String getChange() {
return change;
}
public void setChange(String change) {
this.change = change;
}
public String getHighPrice() {
return highPrice;
}
public void setHighPrice(String highPrice) {
this.highPrice = highPrice;
}
public String getLowPrice() {
return lowPrice;
}
public void setLowPrice(String lowPrice) {
this.lowPrice = lowPrice;
}
public String getVolume() {
return volume;
}
public void setVolume(String volume) {
this.volume = volume;
}
public String getBTCVolume() {
return bTCVolume;
}
public void setBTCVolume(String bTCVolume) {
this.bTCVolume = bTCVolume;
}
public String getTradeCount() {
return tradeCount;
}
public void setTradeCount(String tradeCount) {
this.tradeCount = tradeCount;
}
public String getBidPrice() {
return bidPrice;
}
public void setBidPrice(String bidPrice) {
this.bidPrice = bidPrice;
}
public String getAskPrice() {
return askPrice;
}
public void setAskPrice(String askPrice) {
this.askPrice = askPrice;
}
public String getBuyOrderCount() {
return buyOrderCount;
}
public void setBuyOrderCount(String buyOrderCount) {
this.buyOrderCount = buyOrderCount;
}
public String getSellOrderCount() {
return sellOrderCount;
}
public void setSellOrderCount(String sellOrderCount) {
this.sellOrderCount = sellOrderCount;
}
}
public class Market {
@SerializedName("success")
@Expose
private String success;
@SerializedName("request")
@Expose
private String request;
@SerializedName("message")
@Expose
private String message;
@SerializedName("result")
@Expose
private List<Result> result = null;
public String getSuccess() {
return success;
}
public void setSuccess(String success) {
this.success = success;
}
public String getRequest() {
return request;
}
public void setRequest(String request) {
this.request = request;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<Result> getResult() {
return result;
}
public void setResult(List<Result> result) {
this.result = result;
}
}
答案 0 :(得分:0)
您的Json响应无效。在']'之前有多余的'}'。
json response :- {"success":"1","request":"/api/v1/getmarketsummaries","message":"","result":[{"MarketID":"18","LastPrice":"0.01458900","Change":"-1.10","HighPrice":"0.01497193","LowPrice":"0.01453000","Volume":"5.27365150","BTCVolume":"5.27365150","TradeCount":"344","BidPrice":"0.01458012","AskPrice":"0.01470990","BuyOrderCount":"228","SellOrderCount":"864"},{"MarketID":"43","LastPrice":"0.00004443","Change":"0.00","HighPrice":"0.00000000","LowPrice":"0.00000000","Volume":"0.00000000","BTCVolume":"0.00000000","TradeCount":"0","BidPrice":"0.00003363","AskPrice":"0.00004433","BuyOrderCount":"29","SellOrderCount":"432"}]}
以及结果和市场类的定义也相反。市场类别具有结果列表。因此应将响应解析为Market对象。
Gson gson = new Gson();
Market market = gson.fromJson(response,Market.class);
List<Result> result = market.getResult();