解析多重嵌套json

一般的json解析工具都是解析一层或两层的,多层的不容易兼容

json格式

{

    "ANSWERS": [

        {

            "ANS_MSG_HDR": {

                "MSG_CODE":"0",    //返回程序执行正确或者错误的编码

                "MSG_TEXT":"",       //返回程序执行正确或者错误的信息

                "MSG_LEVEL":"",

                "RESULT_NUM":"2",

                "MSG_ID":"",

                "PACKAGE_FLAG":"0",

                "DATA_ROWS":"1",

                "BPM_ROWS":"0",

                "RUN_TIMES":"50",   //运行时间

                "MSG_TRACE":""

            },

            "ANS_COMM_DATA":     //返回的主要数据

                [

                    {

                       "ACTL_ISSUE_SIZE": "3.2915",

                       "INVEST_CONSU": "",

                        "FEE_CALC_MODE":"",

                        "PROD_DESC":"啦啦啦",

                        "PROD_NAME":"啦啦啦",

                       "NEWEST_NET_VAL": "1.151",

                        "RISK_TYPE":"2",

                       "SELL_STATUS": "2",

                       "FEE_RATE_FEE_CEIL": "",

                       "ISSUE_PERSON_DES": "啦啦啦",

                        ……..

                    }

                ],

                {

                    "BPM_DATA": [ ]

                },

                {

                    "CONFIG": { }

                }

            ]

        }

    ]

}




利用fastjson的parseObject解析后自己组装 

public static CommonResponse convertFromJsonString(String json)

    {
        CommonResponse response = new CommonResponse();
        JSONObject jsonObj = JSONObject.parseObject(json);
        JSONArray jsonArray = (JSONArray)jsonObj.get("ANSWERS");
        List ansList = new ArrayList();
        for (int i = 0; i < jsonArray.size(); i++ )
        {
            ANSWERS answer = new ANSWERS();
            JSONObject innerJsonObj = (JSONObject)jsonArray.get(i);
            JSONObject hdrJsonObj = (JSONObject)innerJsonObj.get("ANS_MSG_HDR");
            ANS_MSG_HDR hdrObj = JSONObject.parseObject(hdrJsonObj.toJSONString(),
                ANS_MSG_HDR.class);
            JSONArray bodyJsonArray = (JSONArray)innerJsonObj.get("ANS_COMM_DATA");
            List bodyList = new ArrayList();
            JSONArray aTumArray = (JSONArray)bodyJsonArray.get(0);
            List aTumList = new ArrayList();
            for (int j = 0; j < aTumArray.size(); j++ )
            {
                JSONObject aTumJsonObj = (JSONObject)aTumArray.get(j);
                ANS_COMM_DATA aTumObj = JSONObject.parseObject(aTumJsonObj.toJSONString(),
                    ANS_COMM_DATA.class);
                aTumList.add(aTumObj);
            }
            bodyList.add(aTumList);
            JSONObject bpmData = (JSONObject)bodyJsonArray.get(1);
            JSONObject configData = (JSONObject)bodyJsonArray.get(2);
            bodyList.add(bpmData);
            bodyList.add(configData);
            answer.setANS_COMM_DATA(bodyList);
            answer.setANS_MSG_HDR(hdrObj);
            ansList.add(answer);
        }
        response.setANSWERS(ansList);
        return response;
    }

你可能感兴趣的:(Java,json解析,json)