关于多重嵌套的JSON数据解析
Posted 上尤流苏
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于多重嵌套的JSON数据解析相关的知识,希望对你有一定的参考价值。
最近项目中需要封装一套复杂的数据模型返回给前端,大致就是一个用户会有多笔订单,每个订单下可能会有多笔保单,
大致的数据模型如下:
为了方面描述,先看一下一个用户下有一条订单,一条订单下有一个保险订单的情况
data: {
" allCommunityTotalContribution ": "全社总盈余贡献单位数",
" mySumContributionNumber ": "我的保单盈余贡献",
" orderDetailModel ": [{
“orderNumber”:“订单号”,
" policyDetailModel ":{
“orderNumber”:“订单号”,
“orderEffectiveTime”:“订单生效时间”,
“insuranceName”: 保险名称,
“nameOfAssured”:“被保险人姓名”,
“policyEffectiveState”:“保单生效状态”,
“surplusContributionNumber”:“对应保单盈余贡献单位数”
},
},
]
},
success: true
此时,需要定义数据库对应的Model
订单层面的list<model>下还有保单层面的model
保单层面下的model定义了保单的相关属性
下面是mapperInterface和mapper.xml文件
自定义一个resultmap,将订单层面的ORDER_NUMBER和该订单号对应的保单号对应,因为是一对多,所以用标签<collection>,,因此在下面的<collection>中需要多定一个属性,用ORDER_NUMBER对应保单
mybati会自动根据自己定义的resultMap去封装参数到model中
返回的参数如下:
{
"data": {
"allCommunityTotalContribution": 10383837.27,
"mySumContributionNumber": 404316.24,
"proportionValue":"占全社总数比例小于万分之0.1",
"orderDetailModel": [
{
"orderEffectiveTime": "2017-05-26",
"orderNumber": "D000001725261991",
"policyDetailModel": [
{
"insuranceCode": "IAMGLTD01A",
"insuranceName": "互信一生终身团体养老年金保险",
"nameOfAssured": "沈静",
"orderNumber": "D000001725261991",
"policyEffectiveState": "1",
"surplusContributionNumber": 101079.06
}
]
},
{
"orderEffectiveTime": "2017-05-26",
"orderNumber": "D000002265263965",
"policyDetailModel": [
{
"insuranceCode": "IAMGLTD01A",
"insuranceName": "互信一生终身团体养老年金保险",
"nameOfAssured": "沈静",
"orderNumber": "D000002265263965",
"policyEffectiveState": "1",
"surplusContributionNumber": 101079.06
}
]
},
{
"orderEffectiveTime": "2017-05-26",
"orderNumber": "D000003875264027",
"policyDetailModel": [
{
"insuranceCode": "IAMGLTD01A",
"insuranceName": "互信一生终身团体养老年金保险",
"nameOfAssured": "沈静",
"orderNumber": "D000003875264027",
"policyEffectiveState": "1",
"surplusContributionNumber": 101079.06
}
]
},
{
"orderEffectiveTime": "2017-05-26",
"orderNumber": "D000003265263561",
"policyDetailModel": [
{
"insuranceCode": "IAMGLTD01A",
"insuranceName": "互信一生终身团体养老年金保险",
"nameOfAssured": "沈静",
"orderNumber": "D000003265263561",
"policyEffectiveState": "1",
"surplusContributionNumber": 101079.06
}
]
}
]
},
"success": true
}
以上是关于关于多重嵌套的JSON数据解析的主要内容,如果未能解决你的问题,请参考以下文章
@RequestBody jackson解析复杂的传入值的一个坑;jackson解析迭代数组;jackson多重数组;jakson数组