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的使用的更多相关文章

  1. 原生态的ajax 及json和gson学习资源

    @RequestMapping(value = "/{id}/view") @jsobody public String viewProject( @PathVariable(&q ...

  2. JSON(3)Google解析Json库Gson

    本文参考 : http://www.cnblogs.com/chenlhuaf/archive/2011/05/01/gson_test.html 1.资料 官网: http://groups.goo ...

  3. 开源 JSON 库解析性能对比( Jackson / Json.simple / Gson )

    Json 已成为当前服务器与 web 应用之间数据传输的公认标准. 微服务及分布式架构经常会使用 Json 来传输此类文件,因为这已经是 webAPI 的事实标准. 不过正如许多我们习以为常的事情一样 ...

  4. json和gson的区别

    json是一种数据格式,便于数据传输.存储.交换gson是一种组件库,可以把java对象数据转换成json数据格式 GSON简单处理JSON json格式经常需要用到,google提供了一个处理jso ...

  5. JSON、GSON

    文章目录 什么是JSON 特点 JSON的数据结构 -- Object JSON的数据结构 -- Array JSON的数据结构 -- 基本类型 构建 JSON 数据 解析 JSON 数据 GSON ...

  6. Google解析Json库Gson

    1.资料 官网: http://groups.google.com/group/google-gson 代码: https://github.com/google/gson jar包下载: http: ...

  7. Android JSON、GSON、FastJson的封装与解析

    声明: 1.本帖只提供代码,不深入讲解原理.如果读者想要深入了解,那就不要在这个帖子上浪费时间了 2.客户端用的是Google官方的Volley访问服务器,具体了解Volley请戳 这里 3.本帖三种 ...

  8. 大话JSON之Gson解析JSON

    (三)解析Json数组(多条Json数据) 比如有如下Json数据: [{'name':'John', 'grade':[{'course':'English','score':100},{'cour ...

  9. JSON和GSON操作json数据

    1,JSON操作json import net.sf.json.JSONArray; import net.sf.json.JSONObject; //json操作数据 public static S ...

随机推荐

  1. flex组合流动布局实例---利用css的order属性改变盒子排列顺序

    flex弹性盒子 <div class="container"> <div class="box yellow"></div> ...

  2. 《挑战程序设计竞赛》2.6 数学问题-素数 AOJ0009 POJ3126 3421 3292 3641

    AOJ0009 http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0009 题意 求不大于n的素数个数. 思路 素数筛法可解,筛法过程中 ...

  3. php自定义函数: 下载本地服务器的大文件

    // 使用方法 $file_path = './a.zip'; // 只能是本地服务器文件, 多大的文件都支持!! down_file($file_path); // 函数参数: 服务器文件路径,下载 ...

  4. gerrit添加appkey以及简单添加分支

    最近团队开放用上gerrit版本项目管理工具,简单说一下appkey配置过程 首先是拿到gerrit分配的账户密码.然后进入到首页,假如是新搭建的应该是没有信息,我这里有一些提交的信息,然后找到右上角 ...

  5. Map,Filter和Reduce

    转自:https://www.aliyun.com/jiaocheng/444967.html?spm=5176.100033.1.13.xms8KG 摘要:Map,Filter和Reduce三个函数 ...

  6. Scrapy(爬虫)基本运行机制

    Scrapy(爬虫)基本运行机制

  7. 上手Keras

    Keras的核心数据是“模型”,模型是一种组织网络层的方式.Keras中主要的模型是Sequential模型,Sequential是一系列网络层按顺序构成的栈. Sequential模型如下: fro ...

  8. C# 委托及匿名函数

    一. 为什么使用委托,代码如下:(注释掉的是没用委托之前的使用方式,没有注释的是使用了委托的) public delegate string DelProStr(string name); class ...

  9. Hbase 学习笔记3----操作以及维护

    一,基本命令: 建表:create 'table','t1','t2'       也可以建表时加coulmn的属性如:create 'table',{NAME => 't1', BLOOMFI ...

  10. rsync+sersync多线程实时同步

    一.sersync优点 1)使用c++编写,对linux系统文件产生的临时文件和重复文件操作会进行过滤,在结合rsync同步的时候,会减少运行时消耗的本地及网络资源,因此速度更快. 2)相比较inot ...