我如何在java中创建一个数据模型来Mapjson对象?

mzillmmw  于 2023-01-22  发布在  Java
关注(0)|答案(2)|浏览(139)

单个json示例对象如下所示:

"Events":[{
      "sport_event_id": "sr:sport_event:27636100",
      "start_date": "2021-06-22T18:00:00+00:00",
      "sport_name": "Soccer",
      "competition_name": "UEFA Champions League",
      "competition_id": "sr:competition:7",
      "season_name": "UEFA Champions League 21/22",
      "competitors": [
        {
          "id": "sr:competitor:37863",
          "name": "SS Folgore Falciano Calcio",
          "country": "San Marino",
          "country_code": "SMR",
          "abbreviation": "FFC",
          "qualifier": "home",
          "gender": "male"
        },
        {
          "id": "sr:competitor:277829",
          "name": "FC Prishtina",
          "country": "Kosovo",
          "country_code": "KOS",
          "abbreviation": "PRI",
          "qualifier": "away",
          "gender": "male"
        }
      ],
      "venue": {
        "id": "sr:venue:8329",
        "name": "Elbasan Arena",
        "capacity": 12500,
        "city_name": "Elbasan",
        "country_name": "Albania",
        "map_coordinates": "41.115875,20.091992",
        "country_code": "ALB"
      },
      "probability_home_team_winner": 2.5,
      "probability_draw": 88.1,
      "probability_away_team_winner": 9.4
    },

请帮助我理解如何做到这一点。我假设我需要使用Gson库和HashMaps。
我在Youtube上看了几个关于如何为json对象做map的视频。但是我所能找到的都是简单的两个字符串的对象。我无法了解如何为这样一个更复杂的json文件做map。如果有人能为这个文件发布一个代码示例,我将非常感谢。

zvokhttg

zvokhttg1#

java中有几个库可用于将json转换为Map
1.第一个流行的库是Gson,它可用于解析json并将其转换为Map,代码如下:

import com.google.gson.Gson;
import java.util.Map;

public static void convert(){
    String json = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
    Gson gson = new Gson();
    Map<String, String> map = gson.fromJson(json, Map.class);
    ...
}

您可以阅读更多关于here方法的信息。
1.另一个流行的库是Jackson

import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;

public static void convert(){
    String json = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
    ObjectMapper objectMapper = new ObjectMapper();
    Map<String, String> map = objectMapper.readValue(json, Map.class);
    ...
}

您可以在here右侧阅读有关该方法的更多信息

4uqofj5v

4uqofj5v2#

我将使用this这样的站点从示例JSON生成Java类(见下文),然后使用JacksonobjectMapper进行反序列化,如下所示:

Root root = objectMapper.readValue(myJsonString, Root.class); */

然后,您可以这样做,以获得第一个事件(SS Folgore Falciano Calcio)的第一个竞争者的名称:

root.get(0).competitors.get(0).name

生成的类结构。您可能应该使字段私有化并添加getter。

public class Root{
    @JsonProperty("Events") 
    public ArrayList<Event> events;
}

public class Competitor{
    public String id;
    public String name;
    public String country;
    public String country_code;
    public String abbreviation;
    public String qualifier;
    public String gender;
}

public class Event{
    public String sport_event_id;
    public Date start_date;
    public String sport_name;
    public String competition_name;
    public String competition_id;
    public String season_name;
    public ArrayList<Competitor> competitors;
    public Venue venue;
    public double probability_home_team_winner;
    public double probability_draw;
    public double probability_away_team_winner;
}

public class Venue{
    public String id;
    public String name;
    public int capacity;
    public String city_name;
    public String country_name;
    public String map_coordinates;
    public String country_code;
}

相关问题