JSON 与List转换类封装
json与list转换小结:
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
/**
* json 与 list相关转换工具类
* @author sgl
*/
public class JsonListUtil { /**
* @Title: getListByJson 将json字符串转换成List对象
* @param json json字符串
* @return List<String>
*/
public static List<String> getListByJson(String json){
try {
if (json == null || "".equals(json)) {
return null;
}
List<String> list = new ArrayList<String>();
Gson gson = new Gson();
list = gson.fromJson(json,
new TypeToken<List<String>>() {
}.getType());
return list;
} catch (Exception e) {
try {
throw new Exception("json to list error..");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
return null;
}
/**
* @Title: getJsonStrByMap
* @Description: 将List对象转换成json字符串
* @param list
* @return String
*/
public static String getJsonStrByList(List<String> list){
try {
Gson gson = new Gson();
String json = gson.toJson(list);
return json;
} catch (Exception e) {
e.getMessage();
}
return null;
} }
JSON 与List转换类封装的更多相关文章
- 将java中Map对象转为有相同属性的类对象(json作为中间转换)
java中Map对象转为有相同属性的类对象(json作为中间转换) 准备好json转换工具类 public class JsonUtil { private static ObjectMapper o ...
- 【转】C#中将JSon数据转换成实体类,将实体类转换成Json
http://wo13145219.iteye.com/blog/2022667 http://json2csharp.chahuo.com/ using System; using System.C ...
- JSON转换类(二)--List转换成Json、对象集合转换Json等
#region List转换成Json /// <summary> /// List转换成Json /// </summary> public static string Li ...
- JSON转换类
MXS&Vincene ─╄OvЁ &0000010 ─╄OvЁ MXS&Vincene MXS&Vincene ─╄OvЁ:今天很残酷,明天更残酷,后天很美好 ...
- Android项目开发全程(四)-- 将网络返回的json字符串轻松转换成listview列表
前面几篇博文介绍了从项目搭建到获取网络字符串,对一个项目的前期整体工作进行了详细的介绍,本篇接着上篇介绍一下怎么样优雅将网络返回的json字符串轻松转换成listview列表. 先上图,看一下效果. ...
- DataTable与DTO对象的简易转换类
在web开发过程中,有时候为了数据传输的方便,比如:后台需要更新前端的ViewModel,此时我们定义一个与前端ViewModel结构一样的DTO对象,从数据层获取数据后,将数据封装成DTO然后序列化 ...
- JSON.net 在实体类中自定义日期的格式
定义日期格式转换类,其继承 IsoDateTimeConverter,代码如下: public class DateTimeConverter : IsoDateTimeConverter { pub ...
- poco json 中文字符,抛异常JSON Exception -->iconv 转换 备忘录。
起因 最近linux服务器通信需要用到json. jsoncpp比较出名,但poco 1.5版本以后已经带有json库,所以决定使用poco::json(linux 上已经用到了poco这一套框架). ...
- 一种从JSON数据创建Java类的高效办法
<一种从JSON数据创建Java类的高效办法> 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs JSON格式的数据经常会遇到,比如调用Web服 ...
随机推荐
- 从头开始编写一个Orchard网上商店模块(2) - 配置您的Orchard开发环境
原文地址:http://skywalkersoftwaredevelopment.net/blog/writing-an-orchard-webshop-module-from-scratch-par ...
- htmlparser 精确提取的一些代码
一. ConnectionManager manager = Page.getConnectionManager(); Parser parser = new Parser(manager .open ...
- 浅谈MS-SQL锁机制
锁的概述 一. 为什么要引入锁 多个用户同时对数据库的并发操作时会带来以下数据不一致的问题: 丢失更新A,B两个用户读同一数据并进行修改,其中一个用户的修改结果破坏了另一个修改的结果,比如订票系统 脏 ...
- HDU4725 The Shortest Path in Nya Graph dij
分析:对于每一层,原来n个点,然后扩展为原来的三倍,每一层扩展一个入点,一个出点,然后跑最短路 注:tmd我把一个n写成m了,然后wa了7次,我都要怀疑人生了 #include<cstdio&g ...
- Growling Gears
http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11587 G Growling GearsThe Best A ...
- qtcreator 与 opencv
参考: http://blog.csdn.net/skeeee/article/details/10585429
- r.js 配置文件 build.js 不完整注释
-----------------------------------------------------------------------r.js 配置文件 example.build.js ...
- delphi 提取字符中的数字
Function Setstring(cString:string):string; {提取数字} VAr i:integer; str:string; begin str:='' ...
- HW5.8
public class Solution { public static void main(String[] args) { System.out.printf("%s\t%s\t%s\ ...
- guslterFS
Installing GlusterFS - a Quick Start Guide Purpose of this document This document is intended to giv ...