分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

JsonUtils

发布时间:2023-09-06 02:24责任编辑:郭大石关键词:暂无标签
package project;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONArray;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;// fastjson 的工具包public class JsonUtils { ???/** ????* json字符串转map集合 ????* ????* @param jsonStr ????* @return ????*/ ???public static HashMap<String, String> json2Map(String jsonStr) { ???????return JSON.parseObject(jsonStr, new HashMap<String, String>().getClass()); ???} ???/** ????* map转json字符串 ????* ????* @param map ????* @return ????*/ ???public static String map2Json(Map<String, String> map) { ???????String jsonStr = JSON.toJSONString(map); ???????return jsonStr; ???} ???/** ????* json字符串转换成对象 ????* ????* @param jsonString ????* @param cls ????* @return ????*/ ???public static <T> T json2Bean(String jsonString, Class<T> cls) { ???????T t = null; ???????try { ???????????t = JSON.parseObject(jsonString, cls); ???????} catch (Exception e) { ???????????e.printStackTrace(); ???????} ???????return t; ???} ???/** ????* 对象转换成json字符串 ????* ????* @param obj ????* @return ????*/ ???public static String bean2Json(Object obj) { ???????return JSON.toJSONString(obj); ???} ???/** ????* json字符串转换成List集合 ????* (需要实体类) ????* ????* @param jsonString ????* @return ????*/ ???public static <T> List<T> json2List(String jsonString, Class cls) { ???????List<T> list = null; ???????try { ???????????list = JSON.parseArray(jsonString, cls); ???????} catch (Exception e) { ???????????e.printStackTrace(); ???????} ???????return list; ???} ???/** ????* json字符串转换成ArrayList集合 ????* (需要实体类) ????* ????* @param jsonString ????* @return ????*/ ???public static <T> ArrayList<T> json2ArrayList(String jsonString, Class cls) { ???????ArrayList<T> list = null; ???????try { ???????????list = (ArrayList<T>) JSON.parseArray(jsonString, cls); ???????} catch (Exception e) { ???????????e.printStackTrace(); ???????} ???????return list; ???} ???/** ????* List集合转换成json字符串 ????* ????* @param obj ????* @return ????*/ ???public static String list2Json(Object obj) { ???????return JSONArray.toJSONString(obj, true); ???} ???/** ????* json转List ????* (不需要实体类) ????* ????* @param jsonStr ????* @return ????*/ ???public static JSONArray json2List(String jsonStr) { ???????return JSON.parseArray(jsonStr); ???}}

JsonUtils

原文地址:https://www.cnblogs.com/gentle-awen/p/10034057.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved