我们有Json,我们尝试将json解析为object,但我们正在获得异常代码,如下所述。
{
"ResultSet": {
"Query": "Peabody",
"Result": [
{
"symbol": "BTU",
"name": "Peabody Energy Corporation",
"exch": "NYQ",
"type": "S",
"exchDisp": "NYSE",
"typeDisp": "Equity"
},
{
"symbol": "BTU.MX",
"name": "Peabody Energy Corporation",
"exch": "MEX",
"type": "S",
"exchDisp": "Mexico",
"typeDisp": "Equity"
},
{
"symbol": "PE5.MU",
"name": "PEABODY ENERGY",
"exch": "MUN",
"type": "S",
"exchDisp": "Munich",
"typeDisp": "Equity"
},
{
"symbol": "PE5.F",
"name": "PEABODY ENERGY",
"exch": "FRA",
"type": "S",
"exchDisp": "Frankfurt",
"typeDisp": "Equity"
},
{
"symbol": "PE5.DU",
"name": "PEABODY ENERGY",
"exch": "DUS",
"type": "S",
"exchDisp": "Dusseldorf Stock Exchange",
"typeDisp": "Equity"
},
{
"symbol": "PE5.SG",
"name": "PEABODY ENERGY",
"exch": "STU",
"type": "S",
"exchDisp": "Stuttgart",
"typeDisp": "Equity"
},
{
"symbol": "PE51.F",
"name": "PEABODY ENERGY",
"exch": "FRA",
"type": "S",
"exchDisp": "Frankfurt",
"typeDisp": "Equity"
},
{
"symbol": "PE51.DU",
"name": "PEABODY ENERGY",
"exch": "DUS",
"type": "S",
"exchDisp": "Dusseldorf Stock Exchange",
"typeDisp": "Equity"
},
{
"symbol": "PE51.MU",
"name": "PEABODY ENERGY",
"exch": "MUN",
"type": "S",
"exchDisp": "Munich",
"typeDisp": "Equity"
},
{
"symbol": "PE51.SG",
"name": "PEABODY ENERGY",
"exch": "STU",
"type": "S",
"exchDisp": "Stuttgart",
"typeDisp": "Equity"
}
]
}
}
我们有课程: -
class CustomJSON {
private String ResultSet;
private String Query;
private List<Result> Result = new ArrayList<Result>();
public String getResultSet() {
return ResultSet;
}
public void setResultSet(String resultSet) {
ResultSet = resultSet;
}
public String getQuery() {
return Query;
}
public void setQuery(String query) {
Query = query;
}
public List<Result> getResult() {
return Result;
}
public void setResult(List<Result> result) {
Result = result;
}
}
class Result{
private String symbol;
private String name;
private String exch;
private String type;
private String exchDisp;
private String typeDisp;
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 getExch() {
return exch;
}
public void setExch(String exch) {
this.exch = exch;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getExchDisp() {
return exchDisp;
}
public void setExchDisp(String exchDisp) {
this.exchDisp = exchDisp;
}
public String getTypeDisp() {
return typeDisp;
}
public void setTypeDisp(String typeDisp) {
this.typeDisp = typeDisp;
}
}
测试: -
CustomJSON customJSON = new Gson().fromJson(result,CustomJSON.class);
但是我们得到了例外: -
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 15
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
at com.google.gson.Gson.fromJson(Gson.java:803)
at com.google.gson.Gson.fromJson(Gson.java:768)
at com.google.gson.Gson.fromJson(Gson.java:717)
at com.google.gson.Gson.fromJson(Gson.java:689)
答案 0 :(得分:1)
根据模型,从
更改json字符串{
"ResultSet": {
"Query": "Peabody",
"Result": [
{
"symbol": "BTU",
"name": "Peabody Energy Corporation",
"exch": "NYQ",
"type": "S",
"exchDisp": "NYSE",
"typeDisp": "Equity"
},
到
{
"ResultSet": "someResultSetString", //<-- this should be string
"Query": "Peabody",
"Result": [
{
"symbol": "BTU",
"name": "Peabody Energy Corporation",
"exch": "NYQ",
"type": "S",
"exchDisp": "NYSE",
"typeDisp": "Equity"
},
ResultSet是String,但您将{
字面值视为对象。删除它并为其分配字符串值。同时删除时请务必删除相应的}
文字。