将特定 JSON 映射到 Java 对象
Mapping specific JSON to Java object
我正在尝试将特定 JSON 映射到我的 Java 模型 class。并且无法将其映射到 Java 对象。
我正在使用 fasterxml (jackson) 将 JSON 映射到我下面的 Java 模型 class - CurrencyModel.java。这个 JSON 开头有 '[' 这可能意味着它是一个数组。我无法将其映射到我的 class、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;
}
}
并且在下面的字符串变量
中有 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 class 有问题。我已经执行了您的输入 json 并附带了下面提到的 java class 映射。
@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;
}
}
还有你的主要 class,
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>>() {
});
您的 json 包含对象列表。所以使用 TypeReference
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}]}]";
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
List<CurrencyModel> currencyModelList = objectMapper.readValue(output, new TypeReference<List<CurrencyModel.Currencies>>(){});
System.out.println(currencyModelList);
我正在尝试将特定 JSON 映射到我的 Java 模型 class。并且无法将其映射到 Java 对象。
我正在使用 fasterxml (jackson) 将 JSON 映射到我下面的 Java 模型 class - CurrencyModel.java。这个 JSON 开头有 '[' 这可能意味着它是一个数组。我无法将其映射到我的 class、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;
}
}
并且在下面的字符串变量
中有 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 class 有问题。我已经执行了您的输入 json 并附带了下面提到的 java class 映射。
@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;
}
}
还有你的主要 class,
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>>() {
});
您的 json 包含对象列表。所以使用 TypeReference
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}]}]";
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
List<CurrencyModel> currencyModelList = objectMapper.readValue(output, new TypeReference<List<CurrencyModel.Currencies>>(){});
System.out.println(currencyModelList);