[英]json Array to bean ( object mapper )
Read the json object and store into the bean by creating the new getter n setter.读取 json object 并通过创建新的 getter n setter 存储到 bean 中。 I want to read the bold value from below json object received as string.我想从下面的 json object 作为字符串接收的粗体值读取。
[{"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; } and other needs to be created
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();
}
I am successfully able to read the country but I want to read other values which are in bold above我能够成功读取国家/地区,但我想读取上面以粗体显示的其他值
CoronaBean
should contain property provinces
, which must be another Bean with properties you want from there. CoronaBean
应该包含属性provinces
,它必须是另一个具有您想要的属性的 Bean。 Simple as that.就那么简单。
Look at the code:看代码:
@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
I think you can also check this question for more details and ways to achive what you need: How to parse JSON in Java我认为您还可以查看此问题以获取更多详细信息和实现所需内容的方法: How to parse JSON in Java
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.