Bean定义:

 public class GetM100DataResponse {
private String service;//接口代码
private String sessionId;//会话Id
private String errorCode;//错误码
private String errorMsg;//错误消息
private String summary;//摘要 private List<M100DataObject> 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<String, Class> classMap = new HashMap<String, Class>();
* 然后在classMap中put你要转换的类中的集合名,如:
*/
Map<String, Class> classMap = new HashMap<String, Class>();
classMap.put("dataPoints", M100DataObject.class);//dataPoints 为 属性名称
/*
* 然后在toBean()的时候把参数加上, 如:
*/
GetM100DataResponse response = (GetM100DataResponse)JSONObject.toBean(json, GetM100DataResponse.class, classMap);
return response;
}

over

JSONObject 转换 JSON复杂对象的更多相关文章

  1. JSONObject转换JSON之将Date转换为指定格式(转)

    项目中,经常会用JSONObject插件将JavaBean或List<JavaBean>转换为JSON格式的字符串,而JavaBean的属性有时候会有java.util.Date这个类型的 ...

  2. JSON转换类(二)--List转换成Json、对象集合转换Json等

    #region List转换成Json /// <summary> /// List转换成Json /// </summary> public static string Li ...

  3. List转换成Json、对象集合转换Json等

    #region List转换成Json /// <summary> /// List转换成Json /// </summary> public static string Li ...

  4. C# 转换Json类

    using System; using System.Collections.Generic; using System.Text; using System.Data; using System.R ...

  5. Java对象转换成xml对象和Java对象转换成JSON对象

    1.把Java对象转换成JSON对象 apache提供的json-lib小工具,它可以方便的使用Java语言来创建JSON字符串.也可以把JavaBean转换成JSON字符串. json-lib的核心 ...

  6. json字符串转json对象,json对象转换成java对象

    @RequestMapping(value = "updateInvestorApplyAccountNo", method = RequestMethod.POST) @Resp ...

  7. FastJson、Jackson、Gson进行Java对象转换Json细节处理

    前言 Java对象在转json的时候,如果对象里面有属性值为null的话,那么在json序列化的时候要不要序列出来呢?对比以下json转换方式 一.fastJson 1.fastJson在转换java ...

  8. 三:C#对象转换Json时的一些高级(特殊)设置;

    导航目录: Newtonsoft.Json 概述 一:Newtonsoft.Json 支持序列化与反序列化的.net 对象类型:    二:C#对象.集合.DataTable与Json内容互转示例: ...

  9. Java基础97 json插件的使用(java对象和json字符串对象之间的转换)

    1.需要用到的包 2.实例 实体类 people package com.shore.entity; /** * @author DSHORE/2019-4-19 * */ public class ...

随机推荐

  1. c++ primer 9 顺序容器

    定义: #include <vector> #include <list> #include <deque> vector<int> svec; lis ...

  2. 【LOJ】#2014. 「SCOI2016」萌萌哒

    题解 这个题好妙啊 首先我们发现,如果我们可以暴力,就是把相同的元素拿并查集合起来,最后统计集合个数\(cnt\) 答案是\(9\*10^{cnt - 1}\) 然而我们做不到= = 我们可以用倍增的 ...

  3. pycharm的安装教程及大坑

    在根据网上的教程创建新工程后,发现不能调用第三方库,网上大多给的是print('hello world'),己适python解释器用的pycharm默认的也不能发现错误.后来浏览了一篇文章才恍然大悟, ...

  4. 【伪暴力+智商剪枝】Codeforces Round #489 (Div. 2) D

    失踪人口突然回归……orz.题解还是有必要写的,虽然估计只有自己(?自己也不一定看得懂)看得懂. 题目链接:http://codeforces.com/contest/992/problem/D 题目 ...

  5. java 读入文件 BufferedReader

    package com.mkyong; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOExcep ...

  6. bash shell 关系

    linux的bash和shell关系 shell通俗理解:把用户输入的命令翻译给操作系统. shell 是一个交互性命令解释器.shell独立于操作系统,这种设计让用户可以灵活选择适合自己的shell ...

  7. XSS漏洞自动化攻击工具XSSer

    XSS漏洞自动化攻击工具XSSer   XSS是Web应用常见的漏洞.利用该漏洞,安全人员在网站注入恶意脚本,控制用户浏览器,并发起其他渗透操作.XSSer是Kali Linux提供的一款自动化XSS ...

  8. ubuntu16.04系统上安装CAJViewer方法步骤教程详解

    下载链接: http://pan.baidu.com/s/1jIqHxLs 或: http://download.csdn.net/detail/arhaiyun/5457947 安装wine1.6: ...

  9. android aar jar

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha aar 是  安卓 类库项目的 二进制发行包.  文件扩展名 是 aar 专家 mave ...

  10. 实用在线小工具 -- Google URL Shortener

          实用在线小工具 -- Google URL Shortener 当你想分享一些你觉得有趣的东西,但是那个链接太长,以至于贴上去一大片.比如在微博上分享一张图片,然后贴上去图片的链接,url ...