博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Json字符串处理
阅读量:6250 次
发布时间:2019-06-22

本文共 5370 字,大约阅读时间需要 17 分钟。

hot3.png

pom.xml

com.google.code.gson
gson
2.7

 编写GsonUtils类

//// Source code recreated from a .class file by IntelliJ IDEA// (powered by Fernflower decompiler)//package util;import com.google.gson.Gson;import com.google.gson.GsonBuilder;import com.google.gson.reflect.TypeToken;import java.lang.reflect.Type;import java.util.Collection;import java.util.Enumeration;import java.util.Iterator;public class GsonUtils {    private static final String EMPTY_JSON = "{}";    private static final String EMPTY_JSON_ARRAY = "[]";    private static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";    public GsonUtils() {    }    public static String toJson(Object target) {        return toJson(target, (Type)null, (String)null);    }    public static String toJson(Object target, Type targetType, String datePattern) {        if(target == null) {            return EMPTY_JSON;        } else {            GsonBuilder builder = new GsonBuilder();            if(datePattern == null || datePattern.length() < 1) {                datePattern = DEFAULT_DATE_PATTERN;            }            builder.setDateFormat(datePattern);            Gson gson = builder.create();            String result = EMPTY_JSON;            try {                if(targetType == null) {                    result = gson.toJson(target);                } else {                    result = gson.toJson(target, targetType);                }            } catch (Exception var7) {                if(target instanceof Collection || target instanceof Iterator || target instanceof Enumeration || target.getClass().isArray()) {                    result = EMPTY_JSON_ARRAY;                }            }            return result;        }    }    public static String toJson(Object target, Type targetType) {        return toJson(target, targetType, (String)null);    }    public static 
T fromJson(String json, TypeToken
token, String datePattern) { if(json != null && json.length() >= 1) { GsonBuilder builder = new GsonBuilder(); if(datePattern == null || datePattern.length() < 1) { datePattern = DEFAULT_DATE_PATTERN; } builder.setDateFormat(datePattern); Gson gson = builder.create(); try { return gson.fromJson(json, token.getType()); } catch (Exception var6) { return null; } } else { return null; } } public static Object fromJson(String json, Type type, String datePattern) { if(json != null && json.length() >= 1) { GsonBuilder builder = new GsonBuilder(); if(datePattern == null || datePattern.length() < 1) { datePattern = DEFAULT_DATE_PATTERN; } builder.setDateFormat(datePattern); Gson gson = builder.create(); try { return gson.fromJson(json, type); } catch (Exception var6) { return null; } } else { return null; } } public static Object fromJson(String json, Type type) { return fromJson(json, (Type)type, (String)null); } public static
T fromJson(String json, TypeToken
token) { return (T)fromJson(json, (TypeToken)token, (String)null); } public static
T fromJson(String json, Class
clazz, String datePattern) { if(json != null && json.length() >= 1) { GsonBuilder builder = new GsonBuilder(); if(datePattern == null || datePattern.length() < 1) { datePattern = DEFAULT_DATE_PATTERN; } builder.setDateFormat(datePattern); Gson gson = builder.create(); try { return gson.fromJson(json, clazz); } catch (Exception var6) { return null; } } else { return null; } } public static
T fromJson(String json, Class
clazz) { return (T)fromJson(json, (Class)clazz, (String)null); }}

编写test测试类

public class Test {    class Model{        private String id;        private String name;        public String getId() {            return id;        }        public void setId(String id) {            this.id = id;        }        public String getName() {            return name;        }        public void setName(String name) {            this.name = name;        }        public String toString(){            return "id="+this.id+";name="+this.name;        }    }    public static void main(String[] args) {        Map
map = new HashMap
(); map.put("1",1); map.put("2",2); map.put("3",3); System.out.println(GsonUtils.toJson(map)); List
listTojson = new ArrayList
(); Test.Model model1 = new Test().new Model(); model1.setId("1"); model1.setName("张三"); Test.Model model2 = new Test().new Model(); model2.setId("2"); model2.setName("李四"); listTojson.add(model1); listTojson.add(model2); String json = GsonUtils.toJson(listTojson); System.out.println("json="+json); List
modelList = (List
)GsonUtils.fromJson(json,new TypeToken
>(){}.getType()); System.out.println("modelList="+modelList.toString()); }}

 

转载于:https://my.oschina.net/zhangshuge/blog/760012

你可能感兴趣的文章
paho.mqtt.embedded-c MQTTPacket transport.c hacking
查看>>
在接口测试中怎么处理开发是否提供接口文档的总结
查看>>
HDU_1086 You can Solve a Geometry Problem too(几何题)
查看>>
Autodesk Infrastructure Modeler (原Galileo伽利略项目)已经正式发布
查看>>
jQuery.Easyui 1.2.4 CHM API 下载
查看>>
我的KT库之----数据库的操作(DbCommandExecutor)
查看>>
在新窗口中打开链接 javascript
查看>>
开发自动化插件地址记录
查看>>
华为 U8800 的国际版、港版、联通版ROM下载地址
查看>>
POJO模式
查看>>
HDU_1398 Square Coins(生成函数)
查看>>
margin-left是做边距,是宽度 left是定位盒子左上角左边位置的一个点
查看>>
VBS基础篇 - 数据类型
查看>>
逃离CSDN
查看>>
47、删除vector中重复元素
查看>>
C# 多线程传参数
查看>>
DFS算法的实现
查看>>
初探React与D3的结合-或许是visualization的新突破?
查看>>
tcpdump抓包以及port查看的一些操作
查看>>
VTK GetScalarPointer() and GetScalarComponentAsFloat() not work
查看>>