zoukankan      html  css  js  c++  java
  • JSONObject 转换 JSON复杂对象

    Bean定义:

    复制代码
     1 public class GetM100DataResponse {
     2     private String service;//接口代码
     3     private String sessionId;//会话Id
     4     private String errorCode;//错误码
     5     private String errorMsg;//错误消息
     6     private String summary;//摘要
     7     
     8     private List<M100DataObject> dataPoints;    //数据列表
     9 
    10     //get set  略
    11 }
    复制代码
    复制代码
     1 public class M100DataObject {
     2     private String dataType;    //数据类型    String
     3     private String sendDateTime;    //发送时间    String
     4     private M100DataObjectKV dataKV;    //数值对象    Object
     5     private String serviceNo;    //用户服务号    String
     6     private Integer userSeq;    //用户序号    Integer
     7     private String eqmtNo;    //设备号    String    
     8     
     9     //get set  略
    10 }
    复制代码

    JSON字符串:

    复制代码
     1 {
     2     "dataPoints":[
     3         {
     4             "dataKV":{
     5                 "pulse":"103",
     6                 "measurementTime":"2015-12-02 12:06:32",
     7                 "low":"91",
     8                 "high":"126",
     9                 "id":"d750fed2-0c95-4722-92ac-3078fa34390b"
    10             },
    11             "dataType":"1",
    12             "eqmtNo":"",
    13             "sendDateTime":"2015-12-02 12:06:33",
    14             "serviceNo":"5716b0badb4b426cbfaaebb1be7d57b3",
    15             "userSeq":"1"
    16         }
    17     ],
    18     "diagResult":"",
    19     "errorCode":"1",
    20     "errorMsg":"成功!",
    21     "propose":"",
    22     "service":"GET_M100_DATA",
    23     "sessionId":"1",
    24     "summary":""
    25 }
    复制代码

    转换代码如下:

    复制代码
     1 public static JsonConfig getDecodeJSONConfig(){
     2     JsonConfig jsonConfig = new JsonConfig();
     3     jsonConfig.registerJsonValueProcessor(String.class, new JsonValueProcessor() {
     4             public Object processArrayValue(Object value,
     5                     JsonConfig arg1) {
     6                 // TODO Auto-generated method stub
     7                 return process(value);
     8             }
     9 
    10             public Object processObjectValue(String key,
    11                     Object value, JsonConfig arg2) {
    12                 // TODO Auto-generated method stub                
    13                 return process(value);   
    14             }
    15 
    16             public Object process(Object value) {
    17                 try {
    18                     if (value instanceof String) {
    19                         return  URLDecoder.decode(value.toString(),"UTF-8");                            
    20                     }
    21                     return value == null ? "" : value.toString();
    22                 } catch (Exception e) {
    23                     return "";
    24                 }
    25             }
    26         }
    27     );
    28     return jsonConfig;
    29 }
    30 public GetM100DataResponse parseData(String resData){//resData为JSON字符串
    31     JsonConfig jsonConfig = getDecodeJSONConfig();
    32     JSONObject json = JSONObject.fromObject(resData, jsonConfig);
    33     /* 
    34      * 在JSONObject.toBean的时候,如果转换的类中有集合,
    35      * 可以先定义:Map<String, Class> classMap = new HashMap<String, Class>();
    36      * 然后在classMap中put你要转换的类中的集合名,如:
    37      */      
    38     Map<String, Class> classMap = new HashMap<String, Class>();
    39     classMap.put("dataPoints", M100DataObject.class);//dataPoints 为 属性名称
    40     /*
    41      * 然后在toBean()的时候把参数加上, 如:
    42      */        
    43     GetM100DataResponse response = (GetM100DataResponse)JSONObject.toBean(json, GetM100DataResponse.class, classMap);
    44     return response;
    45 }
    复制代码

    over

  • 相关阅读:
    两款命令行下的翻译工具: sdcv & translateshell
    谷歌语言标准中,C++成绝对的佼佼者 狼人:
    12个优秀的云计算操作系统 狼人:
    幻灯片在网页设计中应用的21个优秀案例 狼人:
    对Web开发人员有用的8个网站 狼人:
    网页设计师应向肖像画家吸取的11个理念 狼人:
    揭秘Facebook的系统架构 狼人:
    Chrome或取代Firefox成Ubuntu默认浏览器 狼人:
    Gnome 3.2 发布计划及新功能 狼人:
    MyFaces Core v2.0.7/2.1.1 发布,JSF框架 狼人:
  • 原文地址:https://www.cnblogs.com/zouhao/p/10831371.html
Copyright © 2011-2022 走看看