Json字符串處理

2019獨角獸企業重金招聘Python工程師標準>>> hot3.png

pom.xml

<dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>2.7</version>
</dependency>

?編寫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> T fromJson(String json, TypeToken<T> 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> T fromJson(String json, TypeToken<T> token) {return (T)fromJson(json, (TypeToken)token, (String)null);}public static <T> T fromJson(String json, Class<T> 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> T fromJson(String json, Class<T> 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<String,Object> map = new HashMap<String,Object>();map.put("1",1);map.put("2",2);map.put("3",3);System.out.println(GsonUtils.toJson(map));List<Model> listTojson = new ArrayList<Model>();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<Model> modelList = (List<Model>)GsonUtils.fromJson(json,new TypeToken<List<Model>>(){}.getType());System.out.println("modelList="+modelList.toString());}
}

?

轉載于:https://my.oschina.net/zhangshuge/blog/760012

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/397518.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/397518.shtml
英文地址,請注明出處:http://en.pswp.cn/news/397518.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

用腳本控制虛擬機

#############用腳本控制虛擬機給file.sh 一個權限chmod x file.sh轉載于:https://blog.51cto.com/forever8/1863587

HDU 5288

//枚舉因子&#xff0c;查找和i最近的左右是i因子的點即可。#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #define LL long long using namespace std;const int MAX100010; const LL mod1e97; int l_next[10010];…

Git 初步學習

學習目標&#xff1a; 在linux 上安裝Git 服務器 在windows 上安裝 Git 客戶端 創建Git倉庫&#xff0c;git用戶 在windows 中獲取項目&#xff0c;對項目進行增刪改查&#xff0c;更新到服務器 創建兩個分支&#xff0c;進行分支修改和代碼合并 1. 在linux上安裝git服務器 使用…

CRTMPServer 在CentOS 64-bit下的編譯(轉)

CRTMPServer 在CentOS 64-bit下的編譯 http://blog.csdn.net/qiuchangyong/article/details/52848942 一、Centos 用 wget 下載需要的軟件 wget http://www.cmake.org/files/v2.8/cmake-2.8.6.tar.gz 二、安裝 cmake tar zxvf cmake-2.8.4.tar.gzcd cmake-2.8.6./bootstrapgma…

HTML 學習筆記 day one

HTML學習筆記 day one Chapter one 網站開發基礎 1.2網站的基本架構 網站的基本要素&#xff1a;內容&#xff0c;頁面&#xff0c;超鏈接 動態網頁和靜態網頁的區別在于&#xff1a;動態網頁會自動更新&#xff0c;后綴名是.asp或者.aspx;而靜態網頁不會自動更新&#xff0c…

Jquery事件冒泡

事件冒泡 什么是事件冒泡 在一個對象上觸發某類事件&#xff08;比如單擊onclick事件&#xff09;&#xff0c;如果此對象定義了此事件的處理程序&#xff0c;那么此事件就會調用這個處理程序&#xff0c;如果沒有定義此事件處理程序或者事件返回true&#xff0c;那么這個事件會…

WPF對某控件添加右鍵屬性

代碼創建右鍵屬性 ContextMenu cm new ContextMenu();MenuItem mi new MenuItem();mi.Header "打開此文件所有文件夾";mi.Click mi_Click;cm.Items.Add(mi);lv.ContextMenu cm; 轉載于:https://www.cnblogs.com/lunawzh/p/5986356.html

解決虛擬機 正在決定eht0 的ip信息失敗 無鏈接-- 添加虛擬網卡

添加步驟&#xff1a;1、進入設備管理器 2、點下一步3、繼續下一步4、繼續往下走轉載于:https://www.cnblogs.com/Yongzhouunknown/p/4802530.html

jquery元素節點操作

jquery元素節點操作 創建節點 var $div $(<div>); var $div2 $(<div>這是一個div元素</div>); 插入節點 1、append()和appendTo()&#xff1a;在現存元素的內部&#xff0c;從后面插入元素 var $span $(<span>這是一個span元素</span>); $(#d…

8位二進制補碼表示整數的最小值是什么,最大值是什么

最大127,最小 -128補碼表示的數,是沒有正負0的,因此除了最高位的符號位以外,可以表示的數最大為 127,因此最大為 127 而因為 10000000,并不是表示為 -0 因此人家用 1000000表示 -128轉載于:https://www.cnblogs.com/huenchao/p/5988288.html

使用 Arduino 和 LM35 溫度傳感器監測溫度

上一篇玩兒了一下Arduino入門&#xff0c;這次再進一步&#xff0c;用一下LM35溫度傳感器來監測當前溫度。LM35溫度傳感器已經在Arduino入門套件里包含了&#xff0c;就是那個有三個腳的小黑塊兒。 我們先把這些東西連起來。把傳感器查在面包板上&#xff0c;然后按照下面的示意…

快照是什么?揭秘存儲快照的實現

歡迎大家前往騰訊云社區&#xff0c;獲取更多騰訊海量技術實踐干貨哦~ 本文由許登博 發表于云社區專欄 原創聲明&#xff1a;本文首發騰訊云云社區&#xff0c;未經允許&#xff0c;不得轉載 前言 存儲網絡行業協會SNIA&#xff08;StorageNetworking Industry Association&…

MySQL 事物隔離級別

1.什么是事物&#xff1a; 訪問并可能更新數據庫的一個完整的程序執行單元&#xff08;UNIT&#xff09;2、事物必須滿足ACID特性&#xff1a;A&#xff0c;atomic&#xff0c;原子性&#xff0c;要么都提交&#xff0c;要么都失敗&#xff0c;不能一部分成功&#xff0c;一部分…

IIS_各種問題

IIS7中默認是已經加載了腳本映射處理。但今天裝了個WIN7&#xff0c;裝好IIS后卻發現沒有。于是手動去這安裝&#xff0c;在添加html映射時提示&#xff1a;模塊列表中必須要有IsapiModule或cgiModule 因為 IIS 7 采用了更安全的 web.config 管理機制&#xff0c;默認情況下會鎖…

平板涂色

題目描述 CE數碼公司開發了一種名為自動涂色機&#xff08;APM&#xff09;的產品。它能用預定的顏色給一塊由不同尺寸且互不覆蓋的矩形構成的平板涂色。 為了涂色&#xff0c;APM需要使用一組刷子。每個刷子涂一種不同的顏色C。APM拿起一把有顏色C的刷子&#xff0c;并給所有顏…

UVA - 1388 Graveyard 【數學】

題目鏈接 題意&#xff1a; 給一個周長為10000的圓&#xff0c;一開始有n個距離相等的點&#xff0c; 現在要添加m個點使其仍舊保持距離相等的狀態&#xff0c;問最小的移動距離。 思路&#xff1a; 遍歷原來的每一個點&#xff0c;找出離他最近的新的位置。 #include <map&…

Android API中被忽略的幾個函數接口

1. MotionEvent的幾個函數 下面的方法都支持多點觸摸&#xff0c;即可以對單個觸摸點調用下面的方法 1.1 getPressure() 這個api 可以獲取到手指觸摸屏幕時候的壓力,但是需要硬件和驅動支持... 它有助于我們做出更加擬物化的設計&#xff0c;比如&#xff1a; 1. 手繪。可以根據…

error while loading shared libraries: libstdc++.so.6: cannot open shared object file

查看誰提供這個.so yum whatprovides libstdc.so.6 yum install libstdc-4.8.5-28.el7.i686 #安裝上邊查出來的.so 此時如果出錯&#xff0c;最后一行是libstdc-4.8.5-28.el7.i686 ! libstdc-4.8.5-11.el7.x86_64 yum update libstdc-4.8.5-11.el7.x86_64 #更新一下,這個是上…

【轉】為控制臺窗口建立消息隊列

介紹Windows的窗口、消息、子類化和超類化 這篇文章本來只是想介紹一下子類化和超類化這兩個比較“生僻”的名詞。為了敘述的完整性而討論了Windows的窗口和消息&#xff0c;也簡要討論了進程和線程。子類化&#xff08;Subclassing&#xff09;和超類化&#xff08;Superclass…

hightmaps 按地圖上顯示的統計數據

離extjs 至 easyui 到html5到hightchars 再到hightmaps。Exjts和easyui很相似&#xff0c;extjs是重量級的&#xff0c;easyui輕量級的。比extjs容易上手。照著demo改就能夠開發了。easyui入門demo見&#xff1a;easyui-demo&#xff0c;或者到官網http://www.jeasyui.com/&…