解析复杂的json字符串

public Map acceptPrintInfo(String printJson) {
        Map result = new HashMap();
        TranDataBO tranData=new TranDataBO();
        RiskBO risk=new RiskBO();
        CashValueBO cashValue=new CashValueBO();
        AppntBO appnt=new AppntBO();
        InsuredBO insured=new InsuredBO();
        CompanyInfoBO companyInfo=new CompanyInfoBO();
        JSONObject printInfo = JSONObject.fromObject(printJson);
        tranData.setBizNo(printInfo.getString("bizNo"));
        tranData.setComment(printInfo.getString("comment"));
        tranData.setInsurancePlan(printInfo.getString("insurancePlan"));
        tranData.setTotalPremiun(printInfo.getString("totalPremiun"));
        tranData.setImage(printInfo.getString("image"));
        tranData.setTemplateCode(printInfo.getString("templateCode"));
        tranData.setClauseUrl(printInfo.getString("clauseUrl"));
        tranData.setCValiDate(printInfo.getString("CValiDate"));
        tranData.setInsurancePlanNo(printInfo.getString("insurancePlanNo"));
        tranData.setPolSpec(printInfo.getString("polSpec"));
        tranData.setContEndDate(printInfo.getString("contEndDate"));
        JSONArray risksArray = printInfo.getJSONArray("risks");
        /*---------------保险种类------------------------------*/
        Iterator<JSONArray> risks = risksArray.iterator();
        while (risks.hasNext()) {
            JSONObject riskInfo = JSONObject.fromObject(risks.next());
            risk.setRiskName(riskInfo.getString("riskName"));
            risk.setPayMode(riskInfo.getString("payMode"));
            risk.setInsuredAmount(riskInfo.getString("insuredAmount"));
            risk.setPayPeriod(riskInfo.getString("payPeriod"));
            risk.setRiskPeriod(riskInfo.getString("riskPeriod"));
            risk.setPremiun(riskInfo.getString("premiun"));
            JSONArray cashValuesArray = riskInfo.getJSONArray("cashValues");
            Iterator<JSONArray> cashValues = cashValuesArray.iterator();
            while (cashValues.hasNext()) {
                JSONObject cashValueInfo = JSONObject.fromObject(cashValues.next());
                cashValue.setCash(cashValueInfo.getString("cash"));
                cashValue.setEnd(cashValueInfo.getString("end"));
            }
        }
        /*---------------投保人信息------------------------------*/
        JSONObject appntInfo=printInfo.getJSONObject("appnt");
        appnt.setCertNo(appntInfo.getString("certNo"));
        appnt.setCertType(appntInfo.getString("certType"));
        appnt.setName(appntInfo.getString("name"));
        appnt.setNo(appntInfo.getString("no"));
        appnt.setSex(appntInfo.getString("sex"));
        /*---------------被保人信息------------------------------*/
        JSONArray insuredsArray = printInfo.getJSONArray("insureds");
        Iterator<JSONArray> insureds= insuredsArray.iterator();
        while (insureds.hasNext()) {
            JSONObject insuredInfo = JSONObject.fromObject(insureds.next());    
            insured.setCertNo(insuredInfo.getString("certNo"));
            insured.setCertType(insuredInfo.getString("certType"));
            insured.setName(insuredInfo.getString("name"));
            insured.setNo(insuredInfo.getString("no"));
            insured.setSex(insuredInfo.getString("sex"));
        }
        /*---------------公司信息------------------------------*/
        JSONObject company=printInfo.getJSONObject("companyInfo");
        companyInfo.setName(company.getString("name"));
        companyInfo.setPhone(company.getString("phone"));
        companyInfo.setAddress(company.getString("address"));
        companyInfo.setZipCode(company.getString("zipCode"));
        companyInfo.setServiceOrganiza(company.getString("serviceOrganiza"));
        companyInfo.setUrl(company.getString("url"));
        
        return result;
    }

对应的json文件:

{
    "insurancePlan": "",
    "risks": [{
        "riskName": "昆仑健康保险股份有限公司E顺行航空意外伤害保险(2017版)",
        "payMode": "缴费方式",
        "cashValues": [{}],
        "insuredAmount": "1000000.00",
        "payPeriod": "2018-02-15",
        "riskPeriod": "2018-02-15",
        "premiun": "100.00"
    }],
    "appnt": {
        "sex": "男",
        "no": "0105836486",
        "name": "李彦祖",
        "certType": "身份证",
        "certNo": "1231231242321312"
    },
    "totalPremiun": "陆拾万元整",
    "image": "",
    "templateCode": "A168",
    "clauseUrl": "http://www.kunlunhealth.com/templet/default/ShowArticle_xxpl.jsp?id=4776",
    "CValiDate": "2018-02-08",
    "insurancePlanNo": "A168",
    "polSpec": "无",
    "insureds": [{
        "sex": "男",
        "no": "0105836486",
        "name": "李彦祖",
        "certType": "身份证",
        "certNo": "12312312312423123"
    }, {
        "sex": "女",
        "no": "0105836485",
        "name": "韩雪",
        "certType": "身份证",
        "certNo": "12312312312"
    }],
    "contEndDate": "2018-02-15",
    "bizNo": "1708041358198180SW01",
    "comment": "备注",
    "companyInfo": {
        "phone": "(86)4008118899",
        "address": "北京市朝阳区建华南路6号院1号楼卓明大厦1层",
        "zipCode": "100022",
        "name": "昆仑健康北京分公司",
        "serviceOrganiza": "深圳众诚泰保险经纪有限公司",
        "url": "www.kunlunhealth.com"
    }
}

猜你喜欢

转载自my.oschina.net/u/3492343/blog/1618661