关于多重嵌套的JSON数据解析

时间:2022-06-23 02:11:49

最近项目中需要封装一套复杂的数据模型返回给前端,大致就是一个用户会有多笔订单,每个订单下可能会有多笔保单,

大致的数据模型如下:

为了方面描述,先看一下一个用户下有一条订单,一条订单下有一个保险订单的情况

data: {

" allCommunityTotalContribution ": "全社总盈余贡献单位数",

" mySumContributionNumber ": "我的保单盈余贡献",

" orderDetailModel ": [{

“orderNumber”:“订单号”,

" policyDetailModel ":{

“orderNumber”:“订单号”,

“orderEffectiveTime”:“订单生效时间”,

“insuranceName”: 保险名称,

“nameOfAssured”:“被保险人姓名”,

“policyEffectiveState”:“保单生效状态”,

“surplusContributionNumber”:“对应保单盈余贡献单位数”

},
                         },
                ]
},

success: true

此时,需要定义数据库对应的Model

关于多重嵌套的JSON数据解析

订单层面的list<model>下还有保单层面的model

关于多重嵌套的JSON数据解析

保单层面下的model定义了保单的相关属性

关于多重嵌套的JSON数据解析

下面是mapperInterface和mapper.xml文件

自定义一个resultmap,将订单层面的ORDER_NUMBER和该订单号对应的保单号对应,因为是一对多,所以用标签<collection>,,因此在下面的<collection>中需要多定一个属性,用ORDER_NUMBER对应保单

关于多重嵌套的JSON数据解析

关于多重嵌套的JSON数据解析

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
}