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 ...
随机推荐
- About AcitveDirectory EventLog
参考微软文档整理的常用EVENTID: Account Logon Account Management Policy Change Event ID Event message 分類 類別 4670 ...
- JD-GUI
JD-GUI http://jd.benow.ca/ JD-GUI可到官網直接下載.官網除了JD-GUI之外,另提供了Eclipse(JD-Eclipse)和IntelliJ(JD-IntelliJ) ...
- 转!!java泛型
介绍java泛型的一篇文章,通俗易懂! 原文地址:http://www.cnblogs.com/lwbqqyumidi/p/3837629.html 一. 泛型概念的提出(为什么需要泛型)? 首先,我 ...
- time 模块,random模块,os模块
一 :time 模块 python中,通常有几种方式来表示时间: 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们运行“type(t ...
- 编程算法 - 全然背包问题 代码(C)
全然背包问题 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 有n个重量和价值分别为w,v的物品, 从这些物品中挑选出总重量不超过W的物品, 求 ...
- 用python合并N个不同字符集编码的sql文件的实践
背景:我有一项工作任务是将svn某文件夹日常更新的sql文件(归类到日期命名的文件夹中)拿到数据库中运行. 一開始,我是先把sql文件update下来,用notepad++打开,拷贝每个文本的sql语 ...
- PHP Laravel 本地化语言支持
That`s it. 我发如今网上Laravel的学习资料实在是太少了.好多东西须要自己去弄.去理解. 我的方法另一个,就是去github上面下载老外写的Laravel站点,然后拿下来自己执行 ...
- Java AES512加密算法
AES - 高级加密标准: 高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这 ...
- mysql如果主库宕机,如何解决?
两种情况服务器down机,数据库down机 如果此时需要切从库 1.先show processlist\G,查看状态 如果看到两个状态,说明此时的从库和主库是同步的 state: waiting fo ...
- mysql 系列文章推荐
1. mysql日志详细解析 http://www.cnblogs.com/wangkongming/p/3684950.html 2. mysql 主从同步实验 http://pmg ...