JSOnObject変換JSON複雑オブジェクト

7944 ワード

転載:JSOnObject変換JSON複雑オブジェクトBean定義:
public class GetM100DataResponse {
    private String service;//    
    private String sessionId;//  Id
    private String errorCode;//   
    private String errorMsg;//    
    private String summary;//  

    private List dataPoints;    //    

    //get set   
}
public class M100DataObject {
    private String dataType;    //        String
    private String sendDateTime;    //        String
    private M100DataObjectKV dataKV;    //        Object
    private String serviceNo;    //         String
    private Integer userSeq;    //        Integer
    private String eqmtNo;    //       String    

    //get set   
}

JSON文字列:
{
    "dataPoints":[
        {
            "dataKV":{
                "pulse":"103",
                "measurementTime":"2015-12-02 12:06:32",
                "low":"91",
                "high":"126",
                "id":"d750fed2-0c95-4722-92ac-3078fa34390b"
            },
            "dataType":"1",
            "eqmtNo":"",
            "sendDateTime":"2015-12-02 12:06:33",
            "serviceNo":"5716b0badb4b426cbfaaebb1be7d57b3",
            "userSeq":"1"
        }
    ],
    "diagResult":"",
    "errorCode":"1",
    "errorMsg":"  !",
    "propose":"",
    "service":"GET_M100_DATA",
    "sessionId":"1",
    "summary":""
}

変換コードは次のとおりです.
public static JsonConfig getDecodeJSONConfig(){
    JsonConfig jsonConfig = new JsonConfig();
    jsonConfig.registerJsonValueProcessor(String.class, new JsonValueProcessor() {
            public Object processArrayValue(Object value,
                    JsonConfig arg1) {
                // TODO Auto-generated method stub
                return process(value);
            }

            public Object processObjectValue(String key,
                    Object value, JsonConfig arg2) {
                // TODO Auto-generated method stub                
                return process(value);   
            }

            public Object process(Object value) {
                try {
                    if (value instanceof String) {
                        return  URLDecoder.decode(value.toString(),"UTF-8");                            
                    }
                    return value == null ? "" : value.toString();
                } catch (Exception e) {
                    return "";
                }
            }
        }
    );
    return jsonConfig;
}
public GetM100DataResponse parseData(String resData){//resData JSON   
    JsonConfig jsonConfig = getDecodeJSONConfig();
    JSONObject json = JSONObject.fromObject(resData, jsonConfig);
    /* 
     *  JSONObject.toBean   ,          ,
     *      :Map classMap = new HashMap();
     *    classMap put           , :
     */      
    Map classMap = new HashMap();
    classMap.put("dataPoints", M100DataObject.class);//dataPoints       
    /*
     *    toBean()        ,  :
     */        
    GetM100DataResponse response = (GetM100DataResponse)JSONObject.toBean(json, GetM100DataResponse.class, classMap);
    return response;
}