*(00)*

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  613 随笔 :: 0 文章 :: 45 评论 :: 159万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

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

 

posted on   *(00)*  阅读(2966)  评论(1编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示