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 ...
随机推荐
- IO流入门-第十三章-File相关
/* java.io.File 1.File和流无关,不能通过该类完成文件的读写 2.File是文件和目录路径名的抽象变现形式. */ import java.io.*; public class F ...
- unity里无损实现制定物体bloom
虽然标题是实现hdr+bloom,但实际感觉更像是控制单个物体bloom https://blog.csdn.net/zsy654321/article/details/80682651
- linux c编程:互斥锁条件变量
条件变量:等待与信号发送 使用互斥锁虽然可以解决一些资源竞争的问题,但互斥锁只有两种状态(加锁和解锁),这限制了互斥锁的用途. 条件变量(条件锁)也可以解决线程同步和共享资源访问的问题,条件变量是对互 ...
- centos Docker安装前升级内核3.10的方法
首先我虚拟机系统都是Centos 6.5 .ESXI ,后安装devel .ESXI 后来 .ESXI 我所操作的都是虚拟机,但是在真实机上面如何我就不清楚了~~ 大家一定要记得安装步骤,,,不然就是 ...
- ionic真机调试Android报错 - could not read ok from ADB Server * failed to start daemon * error: cannot connect to daemon
在使用真机调试Android程序时,报错如下: could not read ok from ADB Server * failed to start daemon error: cannot con ...
- [intellij]create gradle project
https://intellij-support.jetbrains.com/hc/en-us/community/posts/206806425/comments/206797339
- Python HTML Resolution Demo - SGMLParser & PyQuery
1. SGMLParser: 这里定义了一个Parse类,继承SGMLParser里面的方法.使用一个变量is_h4做标记判定html文件中的h4标签,如果遇到h4标签,则将标签内的内容加入到Pars ...
- while小用
1.使用while打印1 2 3 4 5 6 8 9 10 #!/usr/bin/env python #encoding: utf-8 num = 1 while num < 11: if ...
- Codeforces Round #303 (Div. 2)
A.Toy Cars 题意:给出n辆玩具车两两碰撞的结果,找出没有翻车过的玩具车. 思路:简单题.遍历即可. #include<iostream> #include<cstdio&g ...
- ES6 随记(3.4.1)-- 函数的拓展(参数默认值,扩展运算符)
上一章请见: 1. ES6 随记(1)-- let 与 const 2. ES6 随记(2)-- 解构赋值 3. ES6 随记(3.1)-- 字符串的拓展 4. ES6 随记(3.2)-- 正则的拓展 ...