JsonUtil工具类的封装
来源:互联网 发布:淘宝买塑料枪 编辑:程序博客网 时间:2024/06/07 02:51
首先在app/build.gradle文件中添加依赖:
compile 'com.google.code.gson:gson:2.8.0'//Gson依赖注入
import android.text.TextUtils;import com.google.gson.Gson;import org.json.JSONException;import org.json.JSONObject;import java.util.Iterator;import java.util.Map;/** * Created by Administrator on 2016/11/29. */public class JsonUtil { //json转换成bean类 public static <T> T jsonString2Obj(String json, Class<T> clazz) { if (TextUtils.isEmpty(json) || clazz == null) { return null; } return new Gson().fromJson(json, clazz); } //bean类转换成json字符串 public static String obj2JsonString(Object obj) { if (obj == null) { return null; } return new Gson().toJson(obj); } //map集合转换成json字符串 public static String map2JsonString(Map<String, Object> map) throws JSONException { if (map == null || map.size() == 0) { return null; } Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator(); JSONObject jsonObject = new JSONObject(); while (iterator.hasNext()) { Map.Entry<String, Object> mapNext = iterator.next(); jsonObject.put(mapNext.getKey(), mapNext.getValue()); } if (jsonObject != null) { return jsonObject.toString(); } else { return null; } }}
1 0
- JsonUtil工具类的封装
- 自己封装Jackson的工具类——JsonUtil
- 常用JSON工具类JsonUtil封装
- JSON工具类JSONUtil
- JsonUtil工具类
- Json工具类 ---JsonUtil
- Json工具类JsonUtil
- Java-JsonUtil工具类
- JSONUtil工具类
- JSONUtil工具类
- jsonutil工具类
- Json转换工具类JsonUtil
- 工具类分享之《JsonUtil》
- json 转换工具jsonutil
- 封装的工具类
- jsonutil的使用
- JsonUtil
- JsonUtil
- html007_table
- 【android开发】styles.xml常用的设置属性汇总
- [UML]UML概述
- html008_list
- ssh框架
- JsonUtil工具类的封装
- Linux命令行下加速git clone
- 20161129 Coursera Stanford-MachineLearning/Week6
- receiver开机自启动失败原因
- C/C++代码优化方案
- html009_div
- html010_form
- 万年历(JAVA)
- 插入排序-C语言