如何在Spring Boot中将JSON数组映射到Java类

3

我正在尝试从另一个API获取响应,将其转换为Java类,然后使用Spring Boot将其发送到前端。我有来自外部API的JSON响应,如下所示:

{
    "status": {
        "timestamp": "2023-01-31T14:06:45.210Z",
        "error_code": 0,
        "error_message": null,
    },
    "data": [
        {
            "id": 7982,
            "name": "wc4qtz6py1i",
            "tags": [
                "40rcevshzab",
                "3ja25pufu0z"
            ],
            "quote": {
                "USD": {
                    "price": 0.2,
                },
                "BTC": {
                    "price": 7159,
                }
            }
        },
        {
            "id": 8742,
            "name": "uhso98ca",
            "tags": [
                "84jsjsaesxx",
                "sasdd5dda76"
            ],
            "quote": {
                "USD": {
                    "price": 6,
                },
                "BTC": {
                    "price": 1230,
                }
            }
        }
     ]
 }

我需要使用Spring Boot将所有内容转换为类。但是我应该如何组织它呢? 最关键的问题在于“Data”数组。 目前我有这样的一些东西。
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class MainDTO{
    @JsonProperty("status")
    private Status status;
    @JsonProperty("data")
    private Data data;
}

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class Data {
    Coin[] coins;
}

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class Coin{
    @JsonProperty("id")
    private Integer id;
    @JsonProperty("name")
    private String name;
    private Map<String, Coin> quote;
}

但是它无法正常工作。我遇到了错误:

I have error
Tue Jan 31 16:13:45 EET 2023
There was an unexpected error (type=Internal Server Error, status=500).
Error while extracting response for type [class com.example.myCoolApp.entity.MainDTO] and content type [application/json;charset=utf-8]
org.springframework.web.client.RestClientException: Error while extracting response for type [class com.example.myCoolApp.entity.CryptoDTO] and content type [application/json;charset=utf-8]

你能提供接收这个 JSON 数据的 REST 端点吗? - Roland
2个回答

0
你不需要使用一个Data类,因为你的json中的data字段不是一个对象,而是一组对象,这个数组包含了Coin对象。
请按照以下方式进行更改:
public class MainDTO{
    @JsonProperty("status")
    private Status status;
    @JsonProperty("data")
    private List<Coin> data;
}

0
值得一提的是,由于存在额外的逗号,您示例中的JSON格式不正确。 我认为正确的格式应该像这样:
{
    "status": {
        "timestamp": "2023-01-31T14:06:45.210Z",
        "error_code": 0,
        "error_message": null
    },
    "data": [
        {
            "id": 7982,
            "name": "wc4qtz6py1i",
            "tags": [
                "40rcevshzab",
                "3ja25pufu0z"
            ],
            "quote": {
                "USD": {
                    "price": 0.2
                },
                "BTC": {
                    "price": 7159
                }
            }
        },
        {
            "id": 8742,
            "name": "uhso98ca",
            "tags": [
                "84jsjsaesxx",
                "sasdd5dda76"
            ],
            "quote": {
                "USD": {
                    "price": 6
                },
                "BTC": {
                    "price": 1230
                }
            }
        }
     ]
 }

关于类,我建议您选择以下内容:

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class MainDTO {
    @JsonProperty("status")
    private Status status;
    @JsonProperty("data")
    private List<Coin> coins;
}

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class Status {
    @JsonProperty("timestamp")
    private String timestamp;
    @JsonProperty("error_code")
    private Integer errorCode;
    @JsonProperty("error_message")
    private String errorMessage;
}

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class Coin {
    @JsonProperty("id")
    private Integer id;
    @JsonProperty("name")
    private String name;
    @JsonProperty("tags")
    private List<String> tags;
    @JsonProperty("quote")
    private Map<String, Quote> quote;
}

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class Quote {
    @JsonProperty("price")
    private Double price;
}

JSON响应中的"data"数组是一组硬币,因此MainDTO类中的coins属性应该是List<Coin>类型。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接