简体   繁体   English

如何使用 java 在足球统计数据中获取或排序 java json 数组?

[英]How do i get or sort a java json array in soccer statistics using java?

In the code below, the JSON objects and key match_hometeam_score only deliver to me an undesired result.在下面的代码中,JSON 对象和键match_hometeam_score只向我提供了一个不需要的结果。 I just want to print out all the Chelsea scores and separate them from the Arsenal scores.我只想打印出所有切尔西得分并将它们与阿森纳得分分开。 I haven't been able to do that.我一直无法做到这一点。 I don't want to print out Chelsea and Arsenal scores together.我不想一起打印切尔西和阿森纳的得分。

[
{
"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"
}
]

This is the JSON File.这是 JSON 文件。 Arsenal and Chelsea's score are both printing no matter how hard I tried.无论我多么努力,阿森纳和切尔西的得分都在打印。

This code below is what I tried using to print out the home team:下面的代码是我尝试用来打印主队的代码:

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);
}

I am expecting:我期待:

CHELSEA = 4, 0, 3, 1, 0           
ARSENAL = 1, 2, 2, 2, 0 

As their respective scores.作为他们各自的分数。

Here it is with the comments:这是评论:

    //creating map to store list of scores for a team
    Map<String, List<String>> teamScore = new HashMap<>();
    teamScore.put("Arsenal", new LinkedList<>());
    teamScore.put("Chelsea", new LinkedList<>());


    //iterating over json array and extract teams names and scores and store 
    //scores in the map
    JSONArray parentArray = new JSONArray(json);
    for (int i = 0; i < parentArray.length(); i++) {
        JSONObject json_obj = parentArray.getJSONObject(i);
        String homeTeamName = json_obj.getString("match_hometeam_name");
        String homeTeamScore = json_obj.getString("match_hometeam_score");

        String awayTeamName = json_obj.getString("match_awayteam_name");
        String awayTeamScore = json_obj.getString("match_awayteam_score");

        teamScore.get(homeTeamName).add(homeTeamScore);
        teamScore.get(awayTeamName).add(awayTeamScore);
    }

    //printing the map
    System.out.println(teamScore);

Output:输出:

{Arsenal=[1, 2, 2, 2, 0], Chelsea=[4, 0, 3, 1, 0]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM