Gson解析json字符串,并封装成List<T>
来源:互联网 发布:excel中没有数据有效性 编辑:程序博客网 时间:2024/06/05 08:44
最近一个项目中,需要将从服务器上获取到的JSON字符串转换为对象,大概研究了一下,发现在项目中很多地方都要用到,而且有的时候需要返回的是一个集合,所以写了个方法,留着自用。
public class JsonUtils { /** * 将传入的json字符串按类模板解析成对象 * @param json 需要解析的json字符串 * @param cls 类模板 * @return 解析好的对象 */ public static <T> T getObj(String json,Class<T> cls){ Gson gson = new Gson(); T bean = (T) gson.fromJson(json, cls); return bean; } /** * 将传入的对象解析成json字符串 * @param bean 需要解析的对象 * @return 解析完成的json字符串 */ public static <T> String getJsonString(T bean){ Gson gson = new Gson(); String json = gson.toJson(bean, bean.getClass()); return json; } /** * 将获取到的json字符串转换为对象集合进行返回 * @param jsonData 需要解析的json字符串 * @param cls 类模板 * @return */ public <T> List<T> getObjList(String jsonData,Class<T> cls){ List<T> list = new ArrayList<T>(); if (jsonData.startsWith("[") && jsonData.endsWith("]")) {//当字符串以“[”开始,以“]”结束时,表示该字符串解析出来为集合 //截取字符串,去除中括号 jsonData = jsonData.substring(1, jsonData.length() -1); //将字符串以"},"分解成数组 String[] strArr = jsonData.split("\\},"); //分解后的字符串数组的长度 int strArrLength = strArr.length; //遍历数组,进行解析,将字符串解析成对象 for (int i = 0; i < strArrLength; i++) { String newJsonString = null; if (i == strArrLength -1) { newJsonString = strArr[i]; } else { newJsonString = strArr[i] + "}"; } T bean = getObj(newJsonString, cls); list.add(bean); } } if (list == null || list.size() == 0) { return null; } return list; }}
这里测试一下,JSON字符串有点长,就放在后面了。
public class Test { public static void main(String[] args) { Exposure exp = JsonUtils.getObj(json, Exposure.class); System.out.println("exp = " + exp); String myJson = JsonUtils.getJsonString(exp); System.out.println("myJson = " + myJson); List<User> userList = JsonUtils.getObjList(listJson, User.class); for (User user : userList) { System.out.println("user = " + user); } }}public static String json = "{\"Id\": 9928,\"CustomerId\": 9248,\"Remark\": \"大类其他\",\"ExposureItem\": {\"Id\": 20734,\"Has\": true,\"AllergyNames\": null,\"AllergyNamesStr\": \"化学品,毒物,射线,其他\",\"Other\": \"暴露史其他\",\"PropertyName\": \"ExposureItem\",\"Text1\": null,\"Text2\": null,\"Text3\": null,\"Text4\": null,\"Text5\": null,\"Text6\": null,\"Text7\": null,\"Text8\": null,\"Text9\": null,\"Text10\": null,\"Text11\": null,\"Text12\": null,\"Text13\": null,\"Text14\": null,\"Date\": null,\"Remark\": null},\"OccupationalItem\": {\"Id\": 20735,\"Has\": true,\"AllergyNames\": null,\"AllergyNamesStr\": null,\"Other\": null,\"PropertyName\": \"OccupationalItem\",\"Text1\": \"工种\",\"Text2\": null,\"Text3\": \"毒物种类\",\"Text4\": \"1997\",\"Text5\": \"1\",\"Text6\": \"2\",\"Text7\": \"1998\",\"Text8\": \"3\",\"Text9\": \"4\",\"Text10\": null,\"Text11\": null,\"Text12\": null,\"Text13\": null,\"Text14\": null,\"Date\": null,\"Remark\": null}}"; public static String listJson = "[{\"UserId\":9555,\"Name\":\"测试账号\",\"UserType\":\"user\",\"TypeName\":\"VIP金卡\",\"UserTypeId\":4,\"Sex\":0,\"IDCard\":\"110102198506020034\",\"Country\":\"中国\",\"WorkUnit\":\"四川\",\"Phone\":\"02887654321\",\"Mobile\":\"13901234567\",\"QQ\":\"412455414\",\"Mail\":\"412455414@qq.com\",\"FamilyContact1\":\"张三\",\"FamilyContact2\":\"李四\",\"FamilyContact1Phone\":\"12345678912\",\"FamilyContact2Phone\":\"98765432121\",\"Address\":\"成都市\",\"NearbyHospital\":\"未知\",\"HealthServiceCentre\":\"没有\",\"ResidentType\":2,\"BloodType\":3,\"RHNegative\":2,\"EducationBackground\":5,\"Career\":1,\"Career_Other\":\"\",\"MaritalStatus\":0,\"MedicalPayWayId\":2,\"MedicalPayWay_Other\":\"\",\"SocialSecurityNo\":null,\"CardNo\":\"\",\"Birthday\":\"1985-06-02T00:00:00+08:00\",\"ServiceBuyWayId\":1,\"ServiceBuyWay_Other\":null,\"ExpireTime\":\"2016-03-17T00:00:00+08:00\",\"Height\":\"\",\"Waistline\":\"\",\"Hipline\":\"\",\"Weight\":\"\"},{\"UserId\":9555,\"Name\":\"测试账号\",\"UserType\":\"user\",\"TypeName\":\"VIP金卡\",\"UserTypeId\":4,\"Sex\":0,\"IDCard\":\"110102198506020034\",\"Country\":\"中国\",\"WorkUnit\":\"四川\",\"Phone\":\"02887654321\",\"Mobile\":\"13901234567\",\"QQ\":\"412455414\",\"Mail\":\"412455414@qq.com\",\"FamilyContact1\":\"张三\",\"FamilyContact2\":\"李四\",\"FamilyContact1Phone\":\"12345678912\",\"FamilyContact2Phone\":\"98765432121\",\"Address\":\"成都市\",\"NearbyHospital\":\"未知\",\"HealthServiceCentre\":\"没有\",\"ResidentType\":2,\"BloodType\":3,\"RHNegative\":2,\"EducationBackground\":5,\"Career\":1,\"Career_Other\":\"\",\"MaritalStatus\":0,\"MedicalPayWayId\":2,\"MedicalPayWay_Other\":\"\",\"SocialSecurityNo\":null,\"CardNo\":\"\",\"Birthday\":\"1985-06-02T00:00:00+08:00\",\"ServiceBuyWayId\":1,\"ServiceBuyWay_Other\":null,\"ExpireTime\":\"2016-03-17T00:00:00+08:00\",\"Height\":\"\",\"Waistline\":\"\",\"Hipline\":\"\",\"Weight\":\"\"},{\"UserId\":9555,\"Name\":\"测试账号\",\"UserType\":\"user\",\"TypeName\":\"VIP金卡\",\"UserTypeId\":4,\"Sex\":0,\"IDCard\":\"110102198506020034\",\"Country\":\"中国\",\"WorkUnit\":\"四川\",\"Phone\":\"02887654321\",\"Mobile\":\"13901234567\",\"QQ\":\"412455414\",\"Mail\":\"412455414@qq.com\",\"FamilyContact1\":\"张三\",\"FamilyContact2\":\"李四\",\"FamilyContact1Phone\":\"12345678912\",\"FamilyContact2Phone\":\"98765432121\",\"Address\":\"成都市\",\"NearbyHospital\":\"未知\",\"HealthServiceCentre\":\"没有\",\"ResidentType\":2,\"BloodType\":3,\"RHNegative\":2,\"EducationBackground\":5,\"Career\":1,\"Career_Other\":\"\",\"MaritalStatus\":0,\"MedicalPayWayId\":2,\"MedicalPayWay_Other\":\"\",\"SocialSecurityNo\":null,\"CardNo\":\"\",\"Birthday\":\"1985-06-02T00:00:00+08:00\",\"ServiceBuyWayId\":1,\"ServiceBuyWay_Other\":null,\"ExpireTime\":\"2016-03-17T00:00:00+08:00\",\"Height\":\"\",\"Waistline\":\"\",\"Hipline\":\"\",\"Weight\":\"\"}]";
如有不对的地方,请指教。如有侵权,请指出。交流群:181745436
0 0
- Gson解析json字符串,并封装成List<T>
- Gson 将list封装成json
- 使用Gson将List<T>()转化为json字符串
- Gson-JsonParser解析Json字符串
- Gson 解析复杂json字符串
- 试用Gson 解析json字符串
- Gson->Gson创建json字符串,与解析json字符串
- 利用Gson jar包解析json串组装成list
- android之Gson解析json的封装
- Gson解析和封装json数据
- 使用gson将json字符串转为list
- C#后台把多个字符串结果封装成JSON格式并返回前台解析
- 怎样使用Gson 解析 (deserialize) json字符串
- 用Gson解析复杂的Json字符串
- 使用Gson解析常见json字符串
- gson解析json字符串学习笔记...
- gson 的使用 解析json字符串
- java json字符串解析(Gson)
- STM32 GCC 使用 USB 库出现”undefined reference to _sbrk”问题解决
- ora-02292,ora-02266主键约束问题
- 几大抽屉效果第三方总结
- C++--结构体和类
- leetcode-152 Maximum Product Subarray
- Gson解析json字符串,并封装成List<T>
- 扫雷游戏原理
- hive1.1安装
- java中的IO整理
- 利用事务复制实现大容量表定长字段类型的快速变更
- 【python】脚本001-文件备份(模板)
- [学习记录]NSString字符串操作及个人分析
- Leetcode: Valid Palindromic
- 微信公众平台开发(八) 自定义菜单功能开发