public static void main(String[] args) throws IllegalAccessException, InvocationTargetException {
String str = "[{\"repaymentTime\":\"2019-05-15 10:40:00\",\"repaymentMoney\":\"840\",\"consumption\":[{\"consumptionTime\":\"2019-05-12 15:20:00\",\"consumptionMoney\":\"430\"},{\"consumptionTime\":\"2019-05-13 10:05:00\",\"consumptionMoney\":\"410\"}]"
+ "}, {\"repaymentTime\":\"2019-05-20 10:40:00\",\"repaymentMoney\":\"300\",\"consumption\":[{\"consumptionTime\":\"2019-05-16 11:02:00\",\"consumptionMoney\":\"120\"},{\"consumptionTime\":\"2019-05-18 13:00:00\",\"consumptionMoney\":\"180\"}]}]";
List<Map> list = JSONArray.fromObject(str);
System.out.println(list);
for (Map map: list) {
// map转entity
PlanDTO dto = JSON.parseObject(JSON.toJSONString(map), PlanDTO.class);
System.out.println(dto.getRepaymentTime());
System.out.println(dto.getRepaymentMoney());
JSONArray consumptionArray = (JSONArray)map.get("consumption");
List<Consumption> consumptionList = (List<Consumption>) consumptionArray.toCollection(consumptionArray, Consumption.class);
for (Consumption consumption: consumptionList) {
System.out.println(consumption.getConsumptionTime());
System.out.println(consumption.getConsumptionMoney());
}
}
}
PlanDTO.java
public class PlanDTO {
private String repaymentTime; // 时间
private String repaymentMoney; // 金额
List<Consumption> consumption = new ArrayList<Consumption>(); // 消费
public String getRepaymentTime() {
return repaymentTime;
}
public void setRepaymentTime(String repaymentTime) {
this.repaymentTime = repaymentTime;
}
public String getRepaymentMoney() {
return repaymentMoney;
}
public void setRepaymentMoney(String repaymentMoney) {
this.repaymentMoney = repaymentMoney;
}
public List<Consumption> getConsumption() {
return consumption;
}
public void setConsumption(List<Consumption> consumption) {
this.consumption = consumption;
}
}
Consumption.java
public class Consumption {
private String consumptionTime; // 时间
private String consumptionMoney; // 金额
public String getConsumptionTime() {
return consumptionTime;
}
public void setConsumptionTime(String consumptionTime) {
this.consumptionTime = consumptionTime;
}
public String getConsumptionMoney() {
return consumptionMoney;
}
public void setConsumptionMoney(String consumptionMoney) {
this.consumptionMoney = consumptionMoney;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。