前言
写这篇文章的初衷,是为了自己日常进行json格式转化方便。
当然在方便自己使用的同时,也分享给大家
Fastjson【阿里】
我这里使用的是阿里的 fastjson,此文章以fastjson-1.2.83
版本为例,引入maven依赖如下
1 2 3 4 5 6
| <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.83</version> </dependency>
|
1 2
| import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject;
|
json格式的字符串 转 JSONObject
1 2 3 4
| { "name": "张三", "age": "18" }
|
1 2 3 4 5
| public static void myJson(String str) { JSONObject obj = JSON.parseObject(str); System.out.println(obj); }
|
输出:
1
| {"name":"张三" , "age":"18"}
|
json格式的字符串 转 JSONArray
1 2 3 4 5 6 7 8 9 10 11
| [ { "name": "张三", "age": "18" },
{ "name": "李四", "age": "19" } ]
|
1 2 3 4 5 6 7 8
| public static void myJson(String str) { JSONArray array = JSON.parseArray(str); for(Object object : array){ JSONObject obj = (JSONObject)object; System.out.println(obj); } }
|
输出:
1 2
| {"name":"张三" , "age":"18"} {"name":"李四" , "age":"19"}
|
json格式的字符串 转 JSONObject 、JSONArray
1 2 3 4 5 6
| [ "name":{ "name1": "张三", "name2": "李四" } ]
|
1 2 3 4 5 6 7 8 9
| public static void myJson(String str) { JSONObject jsonObj = JSON.parseObject(str); JSONArray array = JSON.parseArray(jsonObj.getString("name")); for(Object object:array){ JSONObject obj = (JSONObject)object; System.out.println(obj); } }
|
输出:
1 2
| {"name1":"张三"} {"name2":"李四"}
|
json格式的字符串 转 实体类(javaBean)
1 2 3 4
| { "name": "张三", "age": "18" }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class Persion { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
|
1 2 3 4 5
| public static void myJson(String str) { Persion persion = JSON.parseObject(str, new TypeReference<Persion>(){}); System.out.println("name:" + persion.getName() + " , " + "age:" + persion.getAge()); }
|
输出:
map格式 转 JSONObject
1 2 3 4
| { name = 张三, age = 18 }
|
1 2 3 4 5 6 7 8
| public static void myJson(String str) { Map<String, Object> map = new HashMap<String, Object>(); map.put("name","张三"); map.put("age","18"); JSONObject jsonObj=new JSONObject(map); System.out.println(jsonObj.toString()); }
|
输出:
1 2 3 4
| { "name":"张三", "age":"18" }
|
实体类 转 json格式的字符串
1 2 3 4 5
| public static void myJson(Student student) { String str = JSONObject.toJSONString(student); System.out.println(str); }
|
输出:
1 2 3 4
| { "name":"张三" , "age":"18" }
|
实体类转实体类
1 2 3 4
| public static void myJson(Student1 student1) { String str = JSONObject.toJSONString(student); Student2 student2 = JSON.parseObject(str, new TypeReference<Student2>(){}); }
|
提取JsonObject(或 Map)中的 key-value 值
1 2 3 4 5
| { "school": { "小学": "4", "初中": "2", "高中": "1" }, "hospital": { "一甲": "3", "二甲": "1" }, "supermarket": { "万达": "2", "万象": "1" } }
|
获取 JsonObject 的key
1 2 3 4 5 6 7
| public void myJson(JSONObject params){ Map<String, Object> map = params; Set<String> keys = map.keySet(); for (String key : keys) { System.out.println(key + "-----" + map.get(key)); } }
|
输出:
1 2 3
| supermarket-----{万达=2, 万象=1} school-----{小学=4, 初中=2, 高中=1} hospital-----{一甲=3, 二甲=1}
|
获取JsonObject 中 School的 key-value 值
1 2 3 4 5 6 7 8 9
| public void myJson(JSONObject params){ Map<String, Object> map = params; Map<String, String> m1 = (Map<String, String>) map.get("school"); Set<String> keys = m1.keySet(); for (String key : keys) { System.out.println(key+" ----- "+m1.get(key)); } }
|
输出:
1 2 3
| 小学 ----- 4 初中 ----- 2 高中 ----- 1
|
Gson【谷歌】
解决反序列化,如果后台每个属性值为空时,也返回该属性
Maven依赖引入Gson
1 2 3 4 5
| <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.10.1</version> </dependency>
|
需要转jsonStirng的对象
1 2
| Gson gson= new GsonBuilder().serializeNulls().create(); String jsonObject = gson.toJson(object);
|
字符处理
String字符串分隔
1 2 3 4 5
| String namesStr = "山东省,江苏省,广东省,浙江省"; String[] names = namesStr.split(","); for (int i = 0, len = names.length; i < len; i++) { System.out.println(names[i]) }
|
输出