我想使用Java解析JSON,但出现以下错误:com.fasterxml.jackson.databind.exc.MismatchedInputException:com.fasterxml.jackson.databind.exc.MismatchedInputException:无法反序列化{{1}的实例}超出START_OBJECT令牌 在[来源:(字符串)”
我的Json看起来像这样:
java.util.ArrayList<com.footbal.dtoStatistics.Statistics>
我用于解析的类是:
{
"api": {
"results": 16,
"statistics": {
"Shots on Goal": {
"home": "3",
"away": "9"
},
"Shots off Goal": {
"home": "5",
"away": "3"
},
"Total Shots": {
"home": "11",
"away": "16"
},
"Blocked Shots": {
"home": "3",
"away": "4"
},
"Shots insidebox": {
"home": "4",
"away": "14"
},
"Shots outsidebox": {
"home": "7",
"away": "2"
},
"Fouls": {
"home": "10",
"away": "13"
},
"Corner Kicks": {
"home": "7",
"away": "4"
},
"Offsides": {
"home": "2",
"away": "1"
},
"Ball Possession": {
"home": "55%",
"away": "45%"
},
"Yellow Cards": {
"home": "0",
"away": "2"
},
"Red Cards": {
"home": null,
"away": null
},
"Goalkeeper Saves": {
"home": "7",
"away": "1"
},
"Total passes": {
"home": "543",
"away": "436"
},
"Passes accurate": {
"home": "449",
"away": "355"
},
"Passes %": {
"home": "83%",
"away": "81%"
}
}
}
}
public class StatisticsResponse {
Api api;
public Api getApi() {
return api;
}
public void setApi(Api api) {
this.api = api;
}
}
public class Api {
int results;
List<Statistics> statistics;
public int getResults() {
return results;
}
public void setResults(int results) {
this.results = results;
}
public List<Statistics> getStatistics() {
return statistics;
}
public void setStatistics(List<Statistics> statistics) {
this.statistics = statistics;
}
}
public class Statistics {
@JsonAlias({"Shots On Goal"})
Stats ShotsonGoal;
@JsonAlias({"Shots Off Goal"})
Stats ShotsoffGoal;
@JsonAlias({"Total Shots"})
Stats TotalShots;
@JsonAlias({"Blocked Shots"})
Stats BlockedShots;
@JsonAlias({"Shots insidebox"})
Stats Shotsinsidebox;
@JsonAlias({"Shots outsidebox"})
Stats Shotsoutsidebox;
Stats Fouls;
@JsonAlias({"Corner Kicks"})
Stats CornerKicks;
Stats Offsides;
@JsonAlias({"Ball Possession"})
Stats BallPossesion;
@JsonAlias({"Yellow Cards"})
Stats YellowCards;
@JsonAlias({"Red Cards"})
Stats RedCards;
@JsonAlias({"Goalkeeper Saves"})
Stats GoalkeeperSaves;
@JsonAlias({"Total passes"})
Stats Totalpasses;
@JsonAlias({"Passes accurate"})
Stats Passesaccurate;
@JsonAlias({"Passes %"})
Stats Passes;
public Stats getShotsonGoal() {
return ShotsonGoal;
}
public void setShotsonGoal(Stats shotsonGoal) {
ShotsonGoal = shotsonGoal;
}
public Stats getShotsoffGoal() {
return ShotsoffGoal;
}
public void setShotsoffGoal(Stats shotsoffGoal) {
ShotsoffGoal = shotsoffGoal;
}
public Stats getTotalShots() {
return TotalShots;
}
public void setTotalShots(Stats totalShots) {
TotalShots = totalShots;
}
public Stats getBlockedShots() {
return BlockedShots;
}
public void setBlockedShots(Stats blockedShots) {
BlockedShots = blockedShots;
}
public Stats getShotsinsidebox() {
return Shotsinsidebox;
}
public void setShotsinsidebox(Stats shotsinsidebox) {
Shotsinsidebox = shotsinsidebox;
}
public Stats getShotsoutsidebox() {
return Shotsoutsidebox;
}
public void setShotsoutsidebox(Stats shotsoutsidebox) {
Shotsoutsidebox = shotsoutsidebox;
}
public Stats getFouls() {
return Fouls;
}
public void setFouls(Stats fouls) {
Fouls = fouls;
}
public Stats getCornerKicks() {
return CornerKicks;
}
public void setCornerKicks(Stats cornerKicks) {
CornerKicks = cornerKicks;
}
public Stats getOffsides() {
return Offsides;
}
public void setOffsides(Stats offsides) {
Offsides = offsides;
}
public Stats getBallPossesion() {
return BallPossesion;
}
public void setBallPossesion(Stats ballPossesion) {
BallPossesion = ballPossesion;
}
public Stats getYellowCards() {
return YellowCards;
}
public void setYellowCards(Stats yellowCards) {
YellowCards = yellowCards;
}
public Stats getRedCards() {
return RedCards;
}
public void setRedCards(Stats redCards) {
RedCards = redCards;
}
public Stats getGoalkeeperSaves() {
return GoalkeeperSaves;
}
public void setGoalkeeperSaves(Stats goalkeeperSaves) {
GoalkeeperSaves = goalkeeperSaves;
}
public Stats getTotalpasses() {
return Totalpasses;
}
public void setTotalpasses(Stats totalpasses) {
Totalpasses = totalpasses;
}
public Stats getPassesaccurate() {
return Passesaccurate;
}
public void setPassesaccurate(Stats passesaccurate) {
Passesaccurate = passesaccurate;
}
public Stats getPasses() {
return Passes;
}
public void setPasses(Stats passes) {
Passes = passes;
}
public class Stats {
int home;
int away;
public int getHome() {
return home;
}
public void setHome(int home) {
this.home = home;
}
public int getAway() {
return away;
}
public void setAway(int away) {
this.away = away;
}
}
我认为我的问题可能来自统计数组中值的名称包含空格的事实,并且我认为JsonAlias的东西存在问题,但是我不知道如何声明它。
有人可以帮助我进行此解析吗?
答案 0 :(得分:1)
您的Json没有List
的{{1}},它是JSON对象
statistics
如果它是一个列表,应该是
"statistics": {
因此,如果您不想使用列表,请仅使用["statistics": {
,
Statistics
答案 1 :(得分:1)
需要将以下更改应用于现有数据模型:
data() {
return {
selectedTaskIndex: undefined,
selectedTaskID:null, //update this
tasks: []
};
},
更改为created: function() {
this.tasks = api.getTasks();
console.log('set this.tasks in TasksLists.vue to')
console.log(this.tasks);
this.selectedTaskID = this.selectedTaskIDProp; //update this
}
:List<Statistics>
Statistics
public class Api {
int results;
Statistics statistics;
public int getResults() { return results; }
public void setResults(int results) { this.results = results; }
public Statistics getStatistics() { return statistics; }
public void setStatistics(Statistics statistics) {
this.statistics = statistics;
}
}
更新:您可以使用@ JsonDeserialize / @ JsonSerialize注释并实现自定义转换器
StatsPercent
答案 2 :(得分:0)
我认为唯一的出路就是改变
List<Statistics> statistics;
到
Map<String,Stats> statistics;
删除Statistics
类并通过Map