JSON工具类
来源:互联网 发布:物竞化学品数据库 编辑:程序博客网 时间:2024/05/22 08:23
json的解析包主要有两个fastJson和Gson。
package com.codefrom.utils;import java.util.ArrayList;import java.util.List;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.serializer.SerializerFeature;import com.google.gson.Gson;import com.google.gson.GsonBuilder;import com.google.gson.JsonObject;import com.google.gson.reflect.TypeToken;public class JsonUtils {public JsonUtils() {}/** * Gson - 根据json字符串和class返回List<T> * * @param json * @param clazz * @return */public static <T> List<T> toListByGson(String json, Class<T> classOfT) {// Gson g = new Gson();Gson g = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();List<JsonObject> jsonObjs = g.fromJson(json,new TypeToken<List<JsonObject>>() {}.getType());ArrayList<T> listOfT = new ArrayList<T>();for (JsonObject jsonObj : jsonObjs) {listOfT.add((T) new Gson().fromJson(jsonObj, classOfT));}return listOfT;}/** * Gson - 根据json字符串和class返回Object * * @param json * @param classOfT * @return */public static <T> Object toObjectByGson(String json, Class<T> classOfT) {Gson g = new Gson();return g.fromJson(json, classOfT);}/** * FastJson - 根据json字符串和class返回List<T> * * @param json * @param classOfT * @return */public static <T> List<T> toListByFastJson(String json, Class<T> classOfT) {return JSON.parseArray(json, classOfT);}/** * FastJson - 根据json字符串和class返回Object * * @param json * @param classOfT * @return */public static <T> Object toObjectByFastJson(String json, Class<T> classOfT) {return JSON.parseObject(json, classOfT);}/** * Gson - 根据list生成json字符串 * * @param list * @return */public static <T> String fromListByGson(List<T> list) {// Gson g = new Gson();// Gson g = new// GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();Gson g = new GsonBuilder().disableHtmlEscaping().create();String json = g.toJson(list);return json;}public static <T> String fromObjectByGson(Object o) {// Gson g = new Gson();// 使用GsonBuilder创建Gson对象,disableHtmlEscaping可以避免将'转换成\u0027这样的问题Gson g = new GsonBuilder().disableHtmlEscaping().create();String json = g.toJson(o);return json;}public static <T> String fromListByFastJson(List<T> list) {// 使用 SerializerFeature.DisableCircularReferenceDetect// 禁止循环使用,如果不添加,会出现"$ref"这种东西String json = JSON.toJSONString(list,SerializerFeature.DisableCircularReferenceDetect);// String json = JSON.toJSONString(list);return json;}public static <T> String fromObjectByFastJson(Object o) {String json = JSON.toJSONString(o,SerializerFeature.DisableCircularReferenceDetect);return json;}public static void main(String[] args) {// ProjectBean p = new ProjectBean();// p.setTitle("this is title");// p.setTags(new String[]{"tag1","tag2"});// p.setAuthor("I'm the author");//// List<ProjectBean> l = new ArrayList<ProjectBean>();// l.add(p);// l.add(p);//// System.out.println(fromObjectByGson(p));// System.out.println(fromObjectByFastJson(p));// System.out.println(fromListByGson(l));// System.out.println(fromListByFastJson(l));//// ProjectBean p1 = (ProjectBean) toObjectByGson(fromObjectByGson(p),// ProjectBean.class);// ProjectBean p2 = (ProjectBean)// toObjectByFastJson(fromObjectByGson(p), ProjectBean.class);// ProjectBean p3 = (ProjectBean)// toObjectByGson(fromObjectByFastJson(p), ProjectBean.class);// ProjectBean p4 = (ProjectBean)// toObjectByFastJson(fromObjectByFastJson(p), ProjectBean.class);// p1.print();// p2.print();// p3.print();// p4.print();}}
0 0
- JSON-json-lib-工具类
- JSON工具类JSONUtil
- JSON 工具类
- Json对象工具类
- JSON解析工具类
- Json工具类
- JSON工具类
- Json工具类
- JSON操作工具类
- Json工具类
- Json工具类
- 解析json工具类
- JSON解析工具类
- Json转换工具类
- Json处理工具类
- JSON工具类
- JSON 转换工具类
- json工具类
- Android笔记--在切换fragment时,让fragment里的listview实时刷新数据的一种方法
- IT 技能推荐
- STM32F4跑马灯实验(原子)
- C++(提高班)第9周课堂练习
- CSAPP3e - integer and floating point - Data Lab
- JSON工具类
- c++第四次作业
- iOS开发——手势识别器(用手势实现图片旋转和缩小放大)
- UVA 1063|11318|UVALive 3807|Marble Game|搜索
- 命令行下将markdown转换成指定格式
- 通过CNTK处理自然语言模型
- c++第四次作业
- Struts2基础知识
- HTML对象处理工具类