【发布时间】:2019-12-25 02:54:10
【问题描述】:
在下面的代码中,JSON 对象和键 match_hometeam_score 只会向我传递不想要的结果。我只想打印出所有切尔西的分数并将它们与阿森纳的分数分开。我没能做到这一点。我不想一起打印切尔西和阿森纳的比分。
[
{
"match_id": "218349",
"country_id": "165",
"country_name": "Europe",
"league_id": "590",
"league_name": "Europa League",
"match_date": "2019-05-29",
"match_status": "Finished",
"match_time": "21:00",
"match_hometeam_id": "2616",
"match_hometeam_name": "Chelsea",
"match_hometeam_score": "4",
"match_awayteam_id": "2617",
"match_awayteam_name": "Arsenal",
"match_awayteam_score": "1",
"match_hometeam_halftime_score": "0",
"match_awayteam_halftime_score": "0",
"match_live": "0"
},
{
"match_id": "167631",
"country_id": "41",
"country_name": "ENGLAND",
"league_id": "148",
"league_name": "Premier League",
"match_date": "2019-01-19",
"match_status": "Finished",
"match_time": "18:30",
"match_hometeam_id": "2617",
"match_hometeam_name": "Arsenal",
"match_hometeam_score": "2",
"match_awayteam_id": "2616",
"match_awayteam_name": "Chelsea",
"match_awayteam_score": "0",
"match_hometeam_halftime_score": "2",
"match_awayteam_halftime_score": "0",
"match_live": "0"
},
{
"match_id": "114836",
"country_id": "41",
"country_name": "ENGLAND",
"league_id": "148",
"league_name": "Premier League",
"match_date": "2018-08-18",
"match_status": "Finished",
"match_time": "18:30",
"match_hometeam_id": "2616",
"match_hometeam_name": "Chelsea",
"match_hometeam_score": "3",
"match_awayteam_id": "2617",
"match_awayteam_name": "Arsenal",
"match_awayteam_score": "2",
"match_hometeam_halftime_score": "",
"match_awayteam_halftime_score": "",
"match_live": "0"
},
{
"match_id": "70876",
"country_id": "41",
"country_name": "ENGLAND",
"league_id": "8640",
"league_name": "Carabao Cup",
"match_date": "2018-01-24",
"match_status": "Finished",
"match_time": "21:00",
"match_hometeam_id": "2617",
"match_hometeam_name": "Arsenal",
"match_hometeam_score": "2",
"match_awayteam_id": "2616",
"match_awayteam_name": "Chelsea",
"match_awayteam_score": "1",
"match_hometeam_halftime_score": "1",
"match_awayteam_halftime_score": "1",
"match_live": "0"
},
{
"match_id": "68621",
"country_id": "41",
"country_name": "ENGLAND",
"league_id": "8640",
"league_name": "Carabao Cup",
"match_date": "2018-01-10",
"match_status": "Finished",
"match_time": "21:00",
"match_hometeam_id": "2616",
"match_hometeam_name": "Chelsea",
"match_hometeam_score": "0",
"match_awayteam_id": "2617",
"match_awayteam_name": "Arsenal",
"match_awayteam_score": "0",
"match_hometeam_halftime_score": "0",
"match_awayteam_halftime_score": "0",
"match_live": "0"
}
]
这是 JSON 文件。不管我怎么努力,阿森纳和切尔西的比分都在打印。
下面这段代码是我用来打印主队的代码:
int i = parentArray.length();
JSONObject json_obj = parentArray.getJSONObject(i);
String name = json_obj.getString("match_hometeam_score");
if(i > 2) {
System.out.println(name);
}
我期待:
CHELSEA = 4, 0, 3, 1, 0
ARSENAL = 1, 2, 2, 2, 0
作为他们各自的分数。
【问题讨论】:
-
你目前得到什么输出?请输入您的代码minimal reproducible example
-
我的输出是 4, 2, 3, 2, 0