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. [LintCode] O(1)检测2的幂次

    class Solution { public: /* * @param n: An integer * @return: True or false */ bool checkPowerOf2(in ...

  2. cocos2d-X学习之主要类介绍:场景(CCScene)

    场景(CCScene) 类结构: CCScene主要有以下两个函数: bool  init () //初始化函数 static CCScene *  node (void) //生CCScene 作为 ...

  3. Redis 教程

    http://www.runoob.com/redis/redis-tutorial.html Redis系列(一)--安装.helloworld以及读懂配置文件 再开个redis系列,本系列打算不详 ...

  4. SQL架构信息读取

    --架构: select * from information_schema.SCHEMATA --表: select table_name from information_schema.table ...

  5. mysql replace 使用注意,update的时候 删除从表数据

    使用REPLACE插入一条记录时,如果不重复,REPLACE就和INSERT的功能一样,如果有重复记录,REPLACE就使用新记录的值来替换原来的记录值. 使用REPLACE的最大好处就是可以将DEL ...

  6. IO流入门-第三章-FileInputStream_FileOutputStream复制

    利用FileInputStream和FileOutputStreamj进行复制粘贴 /* 文件复制粘贴 */ import java.io.*; public class FileInput_Outp ...

  7. Python计算地图上两点经纬度间的距离

    处理地图数据时,经常需要用到两个地理位置间的距离.比如A点经纬度(110.0123, 23.32435),B点经纬度(129.1344,25.5465),求AB两点之间的距离.我们可以用haversi ...

  8. 第05章—Swagger2打造在线接口文档

    spring boot 系列学习记录:http://www.cnblogs.com/jinxiaohang/p/8111057.html 码云源码地址:https://gitee.com/jinxia ...

  9. pycharm-->github / github-->pycharm

    一.pycharm -->发布到--> github:1.github 账号密码 https://github.com/2.git 下载安装 https://git-scm.com/3.配 ...

  10. rest_framake之视图

    开始,先放大招 一  最原始的写法 前戏之序列化 class AuthorSerializer(serializers.ModelSerializer): class Meta: model = mo ...