将特定JSON映射到Java对象
我试图将特定的JSON映射到我的Java模型类。我很难将它映射到Java对象 我正在使用fasterxml(jackson)将JSON映射到下面的Java模型类-CurrencyModel.Java。这个JSON开头有“[”,这可能意味着它是一个数组。我无法将它映射到我的类CurrencyModel.java中将特定JSON映射到Java对象,java,json,mapping,fasterxml,Java,Json,Mapping,Fasterxml,我试图将特定的JSON映射到我的Java模型类。我很难将它映射到Java对象 我正在使用fasterxml(jackson)将JSON映射到下面的Java模型类-CurrencyModel.Java。这个JSON开头有“[”,这可能意味着它是一个数组。我无法将它映射到我的类CurrencyModel.java中 import com.fasterxml.jackson.annotation.JsonProperty; import lombok.AllArgsConstructor; impor
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;
}
}
我正在尝试使用以下方法运行此代码:
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());
我发现您的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;
}
}
@数据
@诺尔格构装师
@AllArgsConstructor
公共类模型{
@JsonProperty(“表”)
私有字符串表;
@JsonProperty(“否”)
私人字符串编号;
@JsonProperty(“生效日期”)
私有字符串有效日期;
@JsonProperty(“费率”)
私人ArrayList费率;
@资料
@诺尔格构装师
@AllArgsConstructor
静态类电流率{
@JsonProperty(“货币”)
私人字符串货币;
@JsonProperty(“代码”)
私有字符串码;
@JsonProperty(“mid”)
私有字符串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>>() {
});
ObjectMapper ObjectMapper=new ObjectMapper();
“有效日期”是指,“生效日期”是指“生效日期”是指“2013-04-04-02”的,”,”,”生效日期”是指“2013-04-04-02”,以及“汇率”是指:“汇率”是:“,,,,,“汇率”是指:“[货币”货币”是:“,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,3.4048}]}]“;
List myObjects=objectMapper.readValue(输出,新类型引用(){
});
您的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);
ObjectMapper ObjectMapper=new ObjectMapper();
“有效日期”是指,“生效日期”是指“生效日期”是指“2013-04-04-02”的,”,”,”生效日期”是指“2013-04-04-02”,以及“汇率”是指:“汇率”是:“,,,,,“汇率”是指:“[货币”货币”是:“,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,3.4048}]}]“;
setVisibility(PropertyAccessor.FIELD,JsonAutoDetect.Visibility.ANY);
List currencyModelList=objectMapper.readValue(输出,新类型引用(){});
System.out.println(currencyModelList);
此JSON具有['在开始时可能表示它是一个数组。:是的,的确如此。因此尝试将其映射到类没有多大意义。您需要将其映射到数组或集合。数组中的唯一对象有4个属性,其中一个是数组。因此,它与您的类不匹配,该类有两个List类型的属性。我如何在我的object CurrencyModel.java中创建对象数组?或者我应该如何使用其他方法?CurrencyModel中已经有两个列表。您应该只有一个列表,因为在JSON中,只有rates
属性是数组。您需要做的是修复CurrencyModel类,使其与唯一的obje匹配ct在JSON数组中,然后将JSON数组反序列化为列表或CurrencyModel[]。
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>>() {
});
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);