JSON和GSON的使用
JSONObject 处理问题
相关博客参考:https://www.cnblogs.com/free-dom/p/5801866.html
json-lib 和google gson 的使用 TorgCadre res=new TorgCadre();
res.setName(torgcadre.getName());//姓名
res.setMarriage(torgcadre.getMarriage());
res.setWeight(torgcadre.getWeight());
res.setSex(torgcadre.getSex());
res.setJkInfo(torgcadre.getJkInfo());
res.setBirthday(torgcadre.getBirthday());
//JSONObject 处理日期字段问题,把对象转换成json数据
JsonConfig config1 = new JsonConfig();
config1.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor("yyyyMMdd"));
JSONObject jsonObject= JSONObject.fromObject(res,config1); //GSON把对象转换成json数据
Gson gson = new GsonBuilder().setDateFormat("yyyyMMdd").create();
String str = gson.toJson(res);
JSONArray jsonArray=JSONArray.fromObject(str); //JSONArray把集合转换成json数据
List<DepositWork> list=new ArrayList<DepositWork>();
JsonConfig config1 = new JsonConfig();
config1.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor("yyyyMMdd"));
JSONArray jsonArray=JSONArray.fromObject(list,config1);
package com.diamond.web.utils; import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale; import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor; /**
* 对象转json日期处理类
* ClassName: JsonDateValueProcessor
* @Description: TODO
* @author HJJ
* @date 2017-12-18
*/
public class JsonDateValueProcessor implements JsonValueProcessor {
private String format ; public JsonDateValueProcessor() {
super();
} public JsonDateValueProcessor(String format) {
super();
this.format = format;
} public Object processArrayValue(Object paramObject,
JsonConfig paramJsonConfig) {
return process(paramObject);
} public Object processObjectValue(String paramString, Object paramObject,
JsonConfig paramJsonConfig) {
return process(paramObject);
} private Object process(Object value){
if(value instanceof Date){
SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.CHINA);
return sdf.format(value);
}
return value == null ? "" : value.toString();
} }
JSON和GSON的使用的更多相关文章
- 原生态的ajax 及json和gson学习资源
@RequestMapping(value = "/{id}/view") @jsobody public String viewProject( @PathVariable(&q ...
- JSON(3)Google解析Json库Gson
本文参考 : http://www.cnblogs.com/chenlhuaf/archive/2011/05/01/gson_test.html 1.资料 官网: http://groups.goo ...
- 开源 JSON 库解析性能对比( Jackson / Json.simple / Gson )
Json 已成为当前服务器与 web 应用之间数据传输的公认标准. 微服务及分布式架构经常会使用 Json 来传输此类文件,因为这已经是 webAPI 的事实标准. 不过正如许多我们习以为常的事情一样 ...
- json和gson的区别
json是一种数据格式,便于数据传输.存储.交换gson是一种组件库,可以把java对象数据转换成json数据格式 GSON简单处理JSON json格式经常需要用到,google提供了一个处理jso ...
- JSON、GSON
文章目录 什么是JSON 特点 JSON的数据结构 -- Object JSON的数据结构 -- Array JSON的数据结构 -- 基本类型 构建 JSON 数据 解析 JSON 数据 GSON ...
- Google解析Json库Gson
1.资料 官网: http://groups.google.com/group/google-gson 代码: https://github.com/google/gson jar包下载: http: ...
- Android JSON、GSON、FastJson的封装与解析
声明: 1.本帖只提供代码,不深入讲解原理.如果读者想要深入了解,那就不要在这个帖子上浪费时间了 2.客户端用的是Google官方的Volley访问服务器,具体了解Volley请戳 这里 3.本帖三种 ...
- 大话JSON之Gson解析JSON
(三)解析Json数组(多条Json数据) 比如有如下Json数据: [{'name':'John', 'grade':[{'course':'English','score':100},{'cour ...
- JSON和GSON操作json数据
1,JSON操作json import net.sf.json.JSONArray; import net.sf.json.JSONObject; //json操作数据 public static S ...
随机推荐
- apk获取源码(dex2jar、jd-gui)
1.下载dex2jar.zip,并解压文件 2.下载jd-gui.zip,解压文件 3.解压apk文件,找到其中的classes.dex 4.将classes.dex复制到dex2jar.bat所在目 ...
- python之MySQL学习——简单的增删改查封装
1.增删改查封装类MysqlHelper.py import pymysql as ps class MysqlHelper: def __init__(self, host, user, passw ...
- Fiddler 抓包工具使用详解
博客园 首页 新随笔 联系 订阅 管理 随笔 - 20 文章 - 0 评论 - 64 Fiddler 抓包工具总结 阅读目录 1. Fiddler 抓包简介 1). 字段说明 2). Statis ...
- visual studio 2017 编译v140 TRACKER : error TRK0005: Failed to locate: "CL.exe". 系统找不到指定的文件
原因可能是vs2017中配置v140 的编译命令路径有问题 解决方案: 用vs2017的在线安装程序,选择修改 进去后选择单个组建,在编译器.生成工具和运行时里面把vc++2015.3…… 打钩的取消 ...
- phpstorm的设置
1.编码:file encodings 2.怎么让每次新建的php文件取消开头的注释:file and code templates ->php file 去掉那个一串代码就可以了
- HTTP 错误 500.21 - Internal Server Error 解决方案(转)
不久前重新安装了Windows7,在安装了VS2010 开发平台之后,将网站发布到IIS,访问发生如下错误: HTTP 错误 500.21 - Internal Server Error处理程序“Ni ...
- lombok插件使用
1.1 lombok介绍 lombok 是一个可以帮助我们简化java代码编写的工具类,尤其是简化javabean的编写,可以通过采用注解的方式,消除代码中的构造方法,getter/setter等代码 ...
- MIS货物拆包销售的问题
就是不能拆包装销售.比如一箱香烟要一包包的卖,一箱里面有50条,一条里面有10包,而是,要一包一包的卖. 解决方案:入库的时候,记录下包装总量(自动改成数量×50),再附加2条说明字段,第一条说明是一 ...
- Pandas 删除指定列中为NaN的行
定位要删除的行 需求:删除指定列中NaN所在行. 如下图,’open‘ 列中有一行为NaN,定位到它,然后删除. 定位: df[np.isnan(df['open'])].index # 这样即可定位 ...
- 利用VMware克隆 windows 虚拟机需要注意的事项
利用VMware克隆windows虚拟机需要注意的事项--克隆虚拟机 --powershell 在域服务器使用,查看所有的sid dsquery computer|dsget computer -dn ...