后端接收复杂JSON数据

举例:(前端传入对象:包含数组、对象、对象列表)
{
    "sysName": "FSSC",
    "dataId": "22f5df09810150ea",
    "dataVersion": "",
    "returnUrl": "http://fsscdev.sce-re.com/",
    "navigateUrl": "http://fsscdev.sce-re.com/",
    "loginName": "xucc",
    "sysArgs": "合同模版匹配参数",
    "enableFB": false,
    "editorMode": 1,
    "paymentMode": 0,
    "CTMReadOnlyFields": [
        "QianYueLeiXing",
        "FaPiaoLeiXing"
    ],
    "CTMName": "关于 xxx 的采购申请合同",
    "CTMSerialNo": "",
    "CTMFormConfig": {
        "NoValidate": 0,
        "HideMoneyStructures": 0,
        "HidePartyContactAddress": 1,
        "HidePartyContactPhone": 0,
        "HidePartyContactName": 0,
        "HidePartyBankAccount": 0,
        "HidePartyBankName": 0,
        "HidePartyAddress": 0,
        "HidePartyPhone": 0,
        "HidePartyFaRen": 0,
        "HidePartyShuiHao": 0,
        "HideParties": 0,
        "HideRef": 0,
        "HideSN": 0,
        "HideName": 0,
        "HideDateFrom": 0,
        "HideDateTo": 0,
        "HideTax": 0,
        "TaxEditable": 1,
        "PartyShuiHaoEditable": 0,
        "HideComment": 1,
        "InvalidChars": null
    },
    "CTMCompanies": [
        {
            "EID": "",
            "MingChen": "单位名称",
            "FaRen": "法人代表",
            "ShuiHao": "税号",
            "KaiHuHang": "开户行",
            "ZhangHao": "账号",
            "DiZhi": "地址",
            "DianHua": "电话",
            "LianXiRen": "联系人",
            "LianXiRenDianHua": "联系人电话"
        }
    ],
    "CTMMoneyStructures": [
        {
            "Type": "产品采购",
            "Money": 20000.00,
            "Tax": 17.5,
            "Remark": ""
        }
    ],
    "CTMPaymentTask": [
        {
            "PayName": "首付款",
            "PayDate1": "2020-12-01",
            "PayDate2": "2020-12-01",
            "PayMoney": 20000.00,
            "PayTax": 17.5,
            "PayCondition": ""
        }
    ]
}

根据前端传入的对象建立DTO

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Arrays;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class SceRequestDTO {
    @JsonProperty(value = "sysName")
    private String sysName;
    @JsonProperty(value = "dataId")
    private String dataId;
    @JsonProperty(value = "dataVersion")
    private String dataVersion;
    @JsonProperty(value = "returnUrl")
    private String returnUrl;
    @JsonProperty(value = "navigateUrl")
    private String navigateUrl;
    @JsonProperty(value = "loginName")
    private String loginName;
    @JsonProperty(value = "sysArgs")
    private String sysArgs;
    @JsonProperty(value = "enableFB")
    private String enableFB;
    @JsonProperty(value = "editorMode")
    private String editorMode;
    @JsonProperty(value = "paymentMode")
    private String paymentMode;
    @JsonProperty(value = "CTMName")
    private String CTMName;
    @JsonProperty(value = "CTMSerialNo")
    private String CTMSerialNo;

    @JsonProperty(value = "CTMReadOnlyFields")
    private String[] ctmReadOnlyFields;
    @JsonProperty(value = "CTMFormConfig")
    private CTMFormConfig ctmFormConfig;
    @JsonProperty(value = "CTMCompanies")
    List<CTMCompanies> ctmCompaniesList;
    @JsonProperty(value = "CTMMoneyStructures")
    List<CTMMoneyStructures> ctmMoneyStructures;
    @JsonProperty(value = "CTMPaymentTask")
    List<CTMPaymentTask> ctmPaymentTaskList;
}

1. DTO中包含的对象
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CTMFormConfig {
    @JsonProperty(value = "NoValidate")
    private int NoValidate;
    @JsonProperty(value = "HideMoneyStructures")
    private int HideMoneyStructures;
    @JsonProperty(value = "HidePartyContactAddress")
    private int HidePartyContactAddress;
    @JsonProperty(value = "HidePartyContactPhone")
    private int HidePartyContactPhone;
    @JsonProperty(value = "HidePartyContactName")
    private int HidePartyContactName;
    @JsonProperty(value = "HidePartyBankAccount")
    private int HidePartyBankAccount;
    @JsonProperty(value = "HidePartyBankName")
    private int HidePartyBankName;
    @JsonProperty(value = "HidePartyAddress")
    private int HidePartyAddress;
    @JsonProperty(value = "HidePartyPhone")
    private int HidePartyPhone;
    @JsonProperty(value = "HidePartyFaRen")
    private int HidePartyFaRen;
    @JsonProperty(value = "HidePartyShuiHao")
    private int HidePartyShuiHao;
    @JsonProperty(value = "HideParties")
    private int HideParties;
    @JsonProperty(value = "HideRef")
    private int HideRef;
    @JsonProperty(value = "HideSN")
    private int HideSN;
    @JsonProperty(value = "HideName")
    private int HideName;
    @JsonProperty(value = "HideDateFrom")
    private int HideDateFrom;
    @JsonProperty(value = "HideDateTo")
    private int HideDateTo;
    @JsonProperty(value = "HideTax")
    private int HideTax;
    @JsonProperty(value = "TaxEditable")
    private int TaxEditable;
    @JsonProperty(value = "PartyShuiHaoEditable")
    private int PartyShuiHaoEditable;
    @JsonProperty(value = "HideComment")
    private int HideComment;
    @JsonProperty(value = "InvalidChars")
    private String InvalidChars;
}
2. DTO中包含的对象集合
package com.hand.hssp.sce.cont.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CTMCompanies {
    @JsonProperty(value = "EID")
    private String EID;
    @JsonProperty(value = "MingChen")
    private String MingChen;
    @JsonProperty(value = "FaRen")
    private String FaRen;
    @JsonProperty(value = "ShuiHao")
    private String ShuiHao;
    @JsonProperty(value = "KaiHuHang")
    private String KaiHuHang;
    @JsonProperty(value = "ZhangHao")
    private String ZhangHao;
    @JsonProperty(value = "DiZhi")
    private String DiZhi;
    @JsonProperty(value = "DianHua")
    private String DianHua;
    @JsonProperty(value = "LianXiRen")
    private String LianXiRen;
    @JsonProperty(value = "LianXiRenDianHua")
    private String LianXiRenDianHua;

}

3. DTO中包含的对象集合
package com.hand.hssp.sce.cont.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CTMMoneyStructures {
    @JsonProperty(value = "Type")
    private String Type;
    @JsonProperty(value = "Money")
    private String Money;
    @JsonProperty(value = "Tax")
    private String Tax;
    @JsonProperty(value = "Remark")
    private String Remark;
}

4. DTO中包含的对象集合
package com.hand.hssp.sce.cont.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CTMPaymentTask {
    @JsonProperty(value = "PayName")
    private String PayName;
    @JsonProperty(value = "PayDate1")
    private String PayDate1;
    @JsonProperty(value = "PayDate2")
    private String PayDate2;
    @JsonProperty(value = "PayMoney")
    private String PayMoney;
    @JsonProperty(value = "PayTax")
    private String PayTax;
    @JsonProperty(value = "PayCondition")
    private String PayCondition;
}

核心注解

 import com.fasterxml.jackson.annotation.JsonProperty;
 @JsonProperty(value = "对象名或者字段名")

xml字符串格式对象转换到插入

1. 前端发送的xml格式字符串
{
    "xmlData": "<acctRequests recordnum=\"3\" recordsum=\"3\" remitdate=\"20200803\" settamtsum=\"342798.5\" settleno=\"A2020080301\" txnamtsum=\"343000\"><acctRequest><systemcode/><tracedate>20200802</tracedate><tracetime>20200802162934</tracetime><orderno>00143035-20200802162805</orderno><ordertype>02</ordertype><txnamt>160000</txnamt><cod>160000</cod><fee>0</fee><payway>02</payway><settledate>20200802</settledate><settleamount>159990</settleamount><charge>10</charge><cardid>621550*********9540</cardid><bankname>安徽省农村信用社</bankname><settletermid>00143035</settletermid><termid>00143035</termid><postrace>010004</postrace><banktrace>162934117963</banktrace><txntype>01</txntype><codmername>河南优麦食品有限公司</codmername><codmercode>898410159989713</codmercode><bizcomname/><bizcomcode/><dsorderno/><netcode>C10.1446</netcode><netname>广德徐瑞芝</netname><dssn>02</dssn><dsname/><cardtype>01</cardtype></acctRequest><acctRequest><systemcode/><tracedate>20200802</tracedate><tracetime>20200802100439</tracetime><orderno>00143057-20200802100334</orderno><ordertype>02</ordertype><txnamt>33000</txnamt><cod>33000</cod><fee>0</fee><payway>02</payway><settledate>20200802</settledate><settleamount>32818.5</settleamount><charge>181.5</charge><cardid>481699******9239</cardid><bankname>中国光大银行</bankname><settletermid>00143057</settletermid><termid>00143057</termid><postrace>011023</postrace><banktrace>100439369612</banktrace><txntype>01</txntype><codmername>河南优麦食品有限公司</codmername><codmercode>898410159989713</codmercode><bizcomname/><bizcomcode/><dsorderno/><netcode>C10.1459</netcode><netname>六安鼎诚商贸有限责任公司</netname><dssn>02</dssn><dsname/><cardtype>03</cardtype></acctRequest><acctRequest><systemcode/><tracedate>20200802</tracedate><tracetime>20200802111621</tracetime><orderno>00171175-20200802111241</orderno><ordertype>02</ordertype><txnamt>150000</txnamt><cod>150000</cod><fee>0</fee><payway>02</payway><settledate>20200802</settledate><settleamount>149990</settleamount><charge>10</charge><cardid>621775*********6611</cardid><bankname>徽商银行</bankname><settletermid>00171175</settletermid><termid>00171175</termid><postrace>007005</postrace><banktrace>111621587320</banktrace><txntype>01</txntype><codmername>河南优麦食品有限公司</codmername><codmercode>898410159989713</codmercode><bizcomname/><bizcomcode/><dsorderno/><netcode>C10.1448</netcode><netname>泾县章梨子</netname><dssn>02</dssn><dsname/><cardtype>01</cardtype></acctRequest></acctRequests>"
}
2. 后端接口根据key获取value
String xmlData = map.get("xmlData");
3. 获取目标对象并转化为json对象
JSONObject jsonObject = XML.toJSONObject(xmlData).getJSONObject("acctRequests");
4. 将json对象转化成dto对象
AcctRequestsDTO acctRequestsDTO = JSON.parseObject(JSON.toJSONString(jsonObject),AcctRequestsDTO.class);
5. 将dto对象的值复制给entity对象
BeanUtils.copyProperties(acctRequestDTO,accountsDet);
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐