Sping Boot 中JSON数组到Java类的Map

kognpnkq  于 2023-02-01  发布在  Java
关注(0)|答案(2)|浏览(171)

我尝试从另一个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将所有这些转换为类。但是我应该如何组织它呢?最大的问题是关于"数据"数组。目前我有这样的东西。
x一个一个一个一个x一个一个二个一个x一个一个三个一个
但它不起作用。我有错误:

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]
lstz6jyr

lstz6jyr1#

你不需要Data类,因为你的json中的data字段不是一个对象--它是一个对象数组,在你的例子中,它是一个Coin对象数组。
按以下方式更改:

public class MainDTO{
    @JsonProperty("status")
    private Status status;
    @JsonProperty("data")
    private List<Coin> data;
}
zzlelutf

zzlelutf2#

值得一提的是,由于多了逗号,示例中的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”数组是一个coins数组,因此MainDTO类中的coins属性应该是List<Coin>类型。

相关问题