这是我想要转换为POJO的JSON信息 http://45.x.x.65/api/getCoins
不知何故,当我尝试发送帖子请求并尝试将json信息转换为模型时,我得到了这个恼人的错误:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 156 path $.coins[0].currencies[0]
这是我接收json文件的请求代码:
String url = "http://45.x.x.65/api/getCoins";
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>()
{
@Override
public void onResponse(String response) {
// response
Log.d("Response", response);
Gson gson = new Gson();
CoinStats coinStats = gson.fromJson(response, CoinStats.class);
// Toast.makeText(getApplicationContext(),coinStats.getExchange(),Toast.LENGTH_SHORT).show();
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
// error
Log.d("Error.Response", error.getMessage());
}
}
) {
@Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<String, String>();
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("Content-Type","application/json");
return params;
}
};
queue.add(postRequest);
这些是模型文件:
CoinStats
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class CoinStats {
@SerializedName("result")
@Expose
private Boolean result;
@SerializedName("exchange")
@Expose
private String exchange;
@SerializedName("from")
@Expose
private List<String> from = null;
@SerializedName("to")
@Expose
private List<String> to = null;
@SerializedName("coins")
@Expose
private List<Coin> coins = null;
public Boolean getResult() {
return result;
}
public void setResult(Boolean result) {
this.result = result;
}
public String getExchange() {
return exchange;
}
public void setExchange(String exchange) {
this.exchange = exchange;
}
public List<String> getFrom() {
return from;
}
public void setFrom(List<String> from) {
this.from = from;
}
public List<String> getTo() {
return to;
}
public void setTo(List<String> to) {
this.to = to;
}
public List<Coin> getCoins() {
return coins;
}
public void setCoins(List<Coin> coins) {
this.coins = coins;
}
}
硬币
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.Currency;
import java.util.List;
public class Coin {
@SerializedName("symbol")
@Expose
private String symbol;
@SerializedName("name")
@Expose
private String name;
@SerializedName("icon")
@Expose
private String icon;
@SerializedName("currencies")
@Expose
private List<Currency> currencies = null;
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public List<Currency> getCurrencies() {
return currencies;
}
public void setCurrencies(List<Currency> currencies) {
this.currencies = currencies;
}
}
货币
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Currency {
@SerializedName("type")
@Expose
private String type;
@SerializedName("price")
@Expose
private Double price;
@SerializedName("when")
@Expose
private Integer when;
@SerializedName("volume")
@Expose
private Integer volume;
@SerializedName("supply")
@Expose
private Double supply;
@SerializedName("market_cap")
@Expose
private Double marketCap;
@SerializedName("volume_24h")
@Expose
private Double volume24h;
@SerializedName("open_24h")
@Expose
private Double open24h;
@SerializedName("high_24h")
@Expose
private Double high24h;
@SerializedName("low_24h")
@Expose
private Double low24h;
@SerializedName("change_24h")
@Expose
private Double change24h;
@SerializedName("percentage_24h")
@Expose
private Double percentage24h;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Integer getWhen() {
return when;
}
public void setWhen(Integer when) {
this.when = when;
}
public Integer getVolume() {
return volume;
}
public void setVolume(Integer volume) {
this.volume = volume;
}
public Double getSupply() {
return supply;
}
public void setSupply(Double supply) { this.supply = supply; }
public Double getMarketCap() {
return marketCap;
}
public void setMarketCap(Double marketCap) {
this.marketCap = marketCap;
}
public Double getVolume24h() {
return volume24h;
}
public void setVolume24h(Double volume24h) {
this.volume24h = volume24h;
}
public Double getOpen24h() {
return open24h;
}
public void setOpen24h(Double open24h) {
this.open24h = open24h;
}
public Double getHigh24h() {
return high24h;
}
public void setHigh24h(Double high24h) {
this.high24h = high24h;
}
public Double getLow24h() {
return low24h;
}
public void setLow24h(Double low24h) {
this.low24h = low24h;
}
public Double getChange24h() {
return change24h;
}
public void setChange24h(Double change24h) {
this.change24h = change24h;
}
public Double getPercentage24h() {
return percentage24h;
}
public void setPercentage24h(Double percentage24h) {
this.percentage24h = percentage24h;
}
}
答案 0 :(得分:2)
您正在导入java.util.Currency而不是您自己的自定义POJO,这可能就是GSON无法正确构建对象的原因。
答案 1 :(得分:0)
我猜你的json是如何形成的,存在某种问题。应该与硬币对应的jsonString部分未正确转换为List。您是否有机会添加要转换为帖子的jsonstring?