Java json数组到bean(对象映射器)
读取json对象并通过创建新的getter n setter存储到bean中。我想从下面读取作为字符串接收的json对象的粗体值Java json数组到bean(对象映射器),java,spring,spring-boot,jackson,spring-boot-maven-plugin,Java,Spring,Spring Boot,Jackson,Spring Boot Maven Plugin,读取json对象并通过创建新的getter n setter存储到bean中。我想从下面读取作为字符串接收的json对象的粗体值 [{"country":"**India**","provinces":[{**"province":"India","confirmed":265928,"recovered":129095,"deaths":7473,"active":129360**}],"latitude":20.593684,"longitude":78.96288,"date":"2020
[{"country":"**India**","provinces":[{**"province":"India","confirmed":265928,"recovered":129095,"deaths":7473,"active":129360**}],"latitude":20.593684,"longitude":78.96288,"date":"2020-06-08"}]
豆子:
我能够成功地读取国家/地区,但我想读取上面用粗体显示的其他值,这些值应该包含属性
省份,必须是另一个具有您想要的属性的Bean。就这么简单
看看代码:
@JsonIgnoreProperties(ignoreUnknown = true)
public class CoronaBean {
private String country;
private ProvinceBean[] provinces
...getters and setters
}
@JsonIgnoreProperties(ignoreUnknown = true)
public class ProvinceBean {
private Integer confirmed;
private Integer recovered;
...rest you want and getters and setters
我想你也可以查看这个问题,了解更多细节和实现所需的方法:
ObjectMapper mapper = new ObjectMapper();
try {
CoronaBean[] coronaBean = mapper.readValue(json, CoronaBean[].class);
for(CoronaBean c: coronaBean ){
System.out.println(c.getCountry());
}
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
@JsonIgnoreProperties(ignoreUnknown = true)
public class CoronaBean {
private String country;
private ProvinceBean[] provinces
...getters and setters
}
@JsonIgnoreProperties(ignoreUnknown = true)
public class ProvinceBean {
private Integer confirmed;
private Integer recovered;
...rest you want and getters and setters