【问题标题】:Consuming a nested JSON array using Spring Boot and RestTemplate使用 Spring Boot 和 RestTemplate 使用嵌套的 JSON 数组
【发布时间】:2018-08-07 07:46:45
【问题描述】:

我正在尝试使用返回以下 JSON 的 HTTP GET 请求在我的 Spring Boot 应用程序中使用 API。我遇到的问题是“playerentry”级别中包含一个 JSON 数组,其中包含未命名/未命名的球员和球队信息对。对于 Spring,通常会为 JSON 的每一层创建一个 java 类,并使用 @JsonProperty() 注释来指定从 JSON 的哪个部分生成 Java 对象。如果没有 JSON 数组中包含的对的名称,并且不确定如何为 playerentry 数组和包含的数组对正确设置 java 类,我一直无法使用 RestTemplate 和 RestTemplateBuilder 来使用这个 JSON。任何帮助将不胜感激。

{
    "rosterplayers": {
        "lastUpdatedOn": "2018-02-25 4:24:30 PM",
        "playerentry": [
            {
                "player": {
                    "ID": "10138",
                    "LastName": "Abrines",
                    "FirstName": "Alex"
                },
                "team": {
                    "ID": "96",
                    "City": "Oklahoma City",
                    "Name": "Thunder",
                    "Abbreviation": "OKL"
                }
            },
            {
                "player": {
                    "ID": "9466",
                    "LastName": "Acy",
                    "FirstName": "Quincy"
                },
                "team": {
                    "ID": "84",
                    "City": "Brooklyn",
                    "Name": "Nets",
                    "Abbreviation": "BRO"
                }
            },
            {
                "player": {
                    "ID": "9390",
                    "LastName": "Adams",
                    "FirstName": "Steven"
                },
                "team": {
                    "ID": "96",
                    "City": "Oklahoma City",
                    "Name": "Thunder",
                    "Abbreviation": "OKL"
                }
            },
            {
                "player": {
                    "ID": "9375",
                    "LastName": "Afflalo",
                    "FirstName": "Arron"
                },
                "team": {
                    "ID": "103",
                    "City": "Sacramento",
                    "Name": "Kings",
                    "Abbreviation": "SAC"
                }
            },
            {
                "player": {
                    "ID": "9357",
                    "LastName": "Ajinca",
                    "FirstName": "Alexis"
                },
                "team": {
                    "ID": "110",
                    "City": "New Orleans",
                    "Name": "Pelicans",
                    "Abbreviation": "NOP"
                }
            },
            {
                "player": {
                    "ID": "9272",
                    "LastName": "Aldrich",
                    "FirstName": "Cole"
                },
                "team": {
                    "ID": "100",
                    "City": "Minnesota",
                    "Name": "Timberwolves",
                    "Abbreviation": "MIN"
                }
            },
            {
                "player": {
                    "ID": "9480",
                    "LastName": "Aldridge",
                    "FirstName": "LaMarcus"
                },
                "team": {
                    "ID": "106",
                    "City": "San Antonio",
                    "Name": "Spurs",
                    "Abbreviation": "SAS"
                }
            },
            {
                "player": {
                    "ID": "9454",
                    "LastName": "Alexander",
                    "FirstName": "Cliff"
                },
                "team": {
                    "ID": "95",
                    "City": "Orlando",
                    "Name": "Magic",
                    "Abbreviation": "ORL"
                }
            },
            {
                "player": {
                    "ID": "9299",
                    "LastName": "Allen",
                    "FirstName": "Tony"
                },
                "team": {
                    "ID": "107",
                    "City": "Memphis",
                    "Name": "Grizzlies",
                    "Abbreviation": "MEM"
                }
            }
        ]
    }
}

【问题讨论】:

    标签: java json spring rest spring-boot


    【解决方案1】:

    这应该有效

    class Roasterplayers {
        String lastUpdatedOn;
        List<PlayerEntry> playerentry;
    }
    
    
    class PlayerEntry {
        Player player;
        Team team;
    }
    
    class Player {
    
        @JsonProperty("ID")
        String id;
        @JsonProperty("LastName")
        String lastName;
        @JsonProperty("FirstName")
        String firstName;
    }
    
    class Team {
        @JsonProperty("ID")
        String id;
        @JsonProperty("City")
        String city;
        @JsonProperty("Name")
        String name;
        @JsonProperty("Abbreviation")
        String abbreviation;
    }
    

    确保每个字段都有 Setter 和 Getter

    【讨论】:

    • 我尝试了这种结构,但遇到的第一个问题是必须有一个围绕 RosterPlayers 类的包装类来表示整个 JSON。完成此操作后,我就可以访问 JSON。
    • 请详细说明如何在spring-boot应用中使用这个
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-31
    • 2015-05-07
    • 2023-04-03
    • 2021-12-27
    • 2023-02-24
    • 2020-07-01
    相关资源
    最近更新 更多