gpt4 book ai didi

java - 将特定的 JSON 映射到 Java 对象

转载 作者:行者123 更新时间:2023-11-29 04:17:14 25 4
gpt4 key购买 nike

我正在尝试将特定的 JSON 映射到我的 Java 模型类。并且无法将其映射到 Java 对象。

我正在使用 fasterxml (jackson) 将 JSON 映射到我下面的 Java 模型类 - CurrencyModel.java。这个 JSON 以 '[' 开头,这可能意味着它是一个数组。我无法将它映射到我的类 CurrencyModel.java

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

class CurrencyModel {
protected List<Currencies> currencies;
protected List<CurrenciesRates> currenciesRates;

@Data
@NoArgsConstructor
@AllArgsConstructor
static class Currencies {
@JsonProperty("table")
private String table;

@JsonProperty("no")
private String no;

@JsonProperty("effectiveDate")
private String effectiveDate;

@JsonProperty("rates")
private ArrayList<CurrencyRatesModel> rates;
}

@Data
@NoArgsConstructor
@AllArgsConstructor
static class CurrenciesRates {
@JsonProperty("currency")
private String currency;

@JsonProperty("code")
private String code;

@JsonProperty("mid")
private String mid;
}
}

在 String 变量中有下面的 JSON

[
{
"table": "A",
"no": "064/A/NBP/2013",
"effectiveDate": "2013-04-02",
"rates": [
{
"currency": "bat (Tajlandia)",
"code": "THB",
"mid": 0.1108
},
{
"currency": "dolar amerykański",
"code": "USD",
"mid": 3.2552
},
{
"currency": "dolar australijski",
"code": "AUD",
"mid": 3.4048
}
]
}
]

我正在尝试使用以下代码运行此代码:

ObjectMapper objectMapper = new ObjectMapper();
final String output = "[{\"table\": \"A\", \"no\": \"064/A/NBP/2013\",\"effectiveDate\": \"2013-04-02\",\"rates\": [{\"currency\": \"bat (Tajlandia)\",\"code\": \"THB\",\"mid\": 0.1108},{\"currency\": \"dolar amerykański\",\"code\": \"USD\",\"mid\": 3.2552},{\"currency\": \"dolar australijski\",\"code\": \"AUD\",\"mid\": 3.4048}]}]";
List<CurrencyModel> currencyModelList = Arrays.asList(objectMapper.readValue(output, CurrencyModel.class));
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
System.out.println(currencyModelList.toArray());

导致错误:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `` out of START_ARRAY token

最佳答案

我发现你的 java 类有问题。我已经执行了您的输入 json 并附带了下面提到的 java 类映射。

@Data
@NoArgsConstructor
@AllArgsConstructor

public class CurrencyModel {

@JsonProperty("table")
private String table;

@JsonProperty("no")
private String no;

@JsonProperty("effectiveDate")
private String effectiveDate;

@JsonProperty("rates")
private ArrayList<CurrenciesRates> rates;

@Data
@NoArgsConstructor
@AllArgsConstructor
static class CurrenciesRates {
@JsonProperty("currency")
private String currency;

@JsonProperty("code")
private String code;

@JsonProperty("mid")
private String mid;
}
}

还有你的主类,

ObjectMapper objectMapper = new ObjectMapper();
final String output = "[{\"table\": \"A\", \"no\": \"064/A/NBP/2013\",\"effectiveDate\": \"2013-04-02\",\"rates\": [{\"currency\": \"bat (Tajlandia)\",\"code\": \"THB\",\"mid\": 0.1108},{\"currency\": \"dolar amerykański\",\"code\": \"USD\",\"mid\": 3.2552},{\"currency\": \"dolar australijski\",\"code\": \"AUD\",\"mid\": 3.4048}]}]";

List<CurrencyModel> myObjects = objectMapper.readValue(output, new TypeReference<List<CurrencyModel>>() {
});

关于java - 将特定的 JSON 映射到 Java 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51582323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com