Java中並無內置的 JSON 解析,須要使用第三方類庫。經常使用的類庫以下html
1、Gson : 古河開發的JSON 庫java
2、FastJson :阿里巴巴的JSON 庫json
3、JackSon : 社區十分活躍數組
JSONObject 是json 對象,一個鍵對應一個值,例如: {「key」:"value"}ide
1.添加fastJson依賴ui
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.58</version> </dependency>
2.將 List 對象轉換爲 Json 格式的Stringthis
List<User> users= findAllUser(); String str = JSONObject.toJSONString(users);
3.將 Map 對象轉換爲 JsonObjectgoogle
Map<String,Object> map = new HashMap(); map.put("name","zhangsan"); map.put("age","15"); JSONObject jsonObject = new JSONObject(map);
4.將Map 轉換爲 Json 格式的String 字符串spa
String jsonStr = JSON.toJSONString(map);
5.將 json 格式的String 字符串轉換爲 Map .net
String jsonStr = "{\"name\":\"zhangsan\",\"age\":\"15\"}"; JSONObject jsonObject = JSONObject.parseObject(jsonStr); Map<String,Object> map = jsonObject;
6. 將 json 格式的字符串轉換爲 JsonObject
String jsonStr = "{\"name\":\"zhangsan\",\"age\":\"15\"}"; JSONObject jsonObject = JSON.parseObject(jsonStr);
7. 將 json字符串轉換爲 Java對象
String jsonStr = "{\"name\":\"zhangsan\",\"email\":\"15\"}"; User user = JSON.parseObject(jsonStr,User.class);
8. 將 json 字符串轉換爲 JSONArray 數組
String jsonStr = "[{\"name\":\"zhangsan\",\"email\":\"zhangsan@qq.com\"},{\"name\":\"lisi\",\"email\":\"lisi@163.com\"}]"; JSONArray array = JSON.parseArray(jsonStr); for(int i=0;i<array.size();i++){ System.out.println(array.get(i)); }
9. 將Java 對象轉換爲 json 字符串
User user = new User(11,"wangwu","wangwu@163.com","上海"); String str = JSON.toJSONString(user);
10. 將json 字符串轉換爲 Java 實體集合
String jsonStr = "[{\"name\":\"zhangsan\",\"email\":\"zhangsan@qq.com\"},{\"name\":\"lisi\",\"email\":\"lisi@163.com\"}]";
List<User> list = JSON.parseArray(jsonStr,User.class);
11. 將 java 對象轉換爲 JsonObject
User user = new User(11,"wangwu","wangwu@163.com","上海"); JSONObject jsonObject = (JSONObject) JSON.toJSON(user);
1.添加依賴
<dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20180130</version> </dependency>
2.Bean 對象轉換爲 JSONObject
User user = new User(11,"wangwu","wangwu@163.com","上海"); JSONObject jsonObject = new JSONObject(user);
3.Map 集合轉換爲 JSONObject 對象
Map<String,Object> map = new HashMap(); map.put("name","zhangsan"); map.put("age","15"); JSONObject jsonObject = new JSONObject(map);
4.JSONObject 轉換爲 Map<String,Object>對象
JSONObject jsonObject = new JSONObject();
jsonObject.toMap();
1.添加依賴
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.3.1</version> </dependency>
2.Gson 的兩個基礎方法
toJson(); fromJson();
3.建立方式一:
// 使用new方法 Gson gson = new Gson(); // toJson 將bean對象轉換爲json字符串 String jsonStr = gson.toJson(user, User.class); // fromJson 將json字符串轉爲bean對象 Student user= gson.fromJson(jsonStr, User.class); // **序列化List** String jsonStr2 = gson.toJson(list); // **反序列化成List時須要使用到TypeToken getType()** List<User> retList = gson.fromJson(jsonStr2,new TypeToken<List<User>>(){}.getType());
建立方式二:
//serializeNulls()是GsonBuilder提供的一種配置,當字段值爲空或null時,依然對該字段進行轉換 Gson gson = new GsonBuilder().serializeNulls().create();
使用GsonBuilder建立Gson實例的步驟:
附錄00:
1 package entity; 2 3 /** 4 * Created by baizhuang on 2019/9/23 16:29. 5 */ 6 7 public class User { 8 private int id; 9 private String name; 10 private String email; 11 private String address; 12 13 public User() { 14 } 15 16 public User(int id, String name, String email, String address) { 17 this.id = id; 18 this.name = name; 19 this.email = email; 20 this.address = address; 21 } 22 23 @Override 24 public String toString() { 25 return "User{" + 26 "id=" + id + 27 ", name='" + name + '\'' + 28 ", email='" + email + '\'' + 29 ", address='" + address + '\'' + 30 '}'; 31 } 32 33 public int getId() { 34 return id; 35 } 36 37 public void setId(int id) { 38 this.id = id; 39 } 40 41 public String getName() { 42 return name; 43 } 44 45 public void setName(String name) { 46 this.name = name; 47 } 48 49 public String getEmail() { 50 return email; 51 } 52 53 public void setEmail(String email) { 54 this.email = email; 55 } 56 57 public String getAddress() { 58 return address; 59 } 60 61 public void setAddress(String address) { 62 this.address = address; 63 } 64 }
附錄01:
1 package entity; 2 3 import com.alibaba.fastjson.JSON; 4 import com.alibaba.fastjson.JSONArray; 5 import com.alibaba.fastjson.JSONObject; 6 import java.util.Arrays; 7 import java.util.HashMap; 8 import java.util.List; 9 import java.util.Map; 10 11 /** 12 * Created by baizhuang on 2019/9/23 16:30. 13 */ 14 15 public class Test { 16 17 public static List<User> findAllUser(){ 18 return Arrays.asList( 19 new User(1,"zhangsan","zhang","北京"), 20 new User(2,"lisi","li","上海")); 21 22 } 23 24 25 public static void listToJsonString(){ 26 List<User> users= findAllUser(); 27 28 System.out.println(users); 29 30 String str = JSONObject.toJSONString(users); 31 32 System.out.println(str); 33 } 34 35 36 public static void maptoJsonObject(){ 37 Map<String,Object> map = new HashMap(); 38 map.put("name","zhangsan"); 39 map.put("age","15"); 40 JSONObject jsonObject = new JSONObject(map); 41 System.out.printf("Json:"+jsonObject); 42 43 String jsonStr = JSON.toJSONString(map); 44 System.out.println(jsonStr); 45 46 } 47 48 49 public static void stringToMap(){ 50 51 String jsonStr = "{\"name\":\"zhangsan\",\"age\":\"15\"}"; 52 53 JSONObject jsonObject = JSONObject.parseObject(jsonStr); 54 55 Map<String,Object> map = jsonObject; 56 57 } 58 59 60 public static void jsonToJsonObject(){ 61 String jsonStr = "{\"name\":\"zhangsan\",\"age\":\"15\"}"; 62 63 JSONObject jsonObject = JSON.parseObject(jsonStr); 64 65 System.out.println(jsonObject); 66 } 67 68 public static void jsonStrToJsonBean(){ 69 String jsonStr = "{\"name\":\"zhangsan\",\"email\":\"15\"}"; 70 71 User user = JSON.parseObject(jsonStr,User.class); 72 73 System.out.println(user); 74 } 75 76 public static void jsonStrToJsonArray(){ 77 String jsonStr = "[{\"name\":\"zhangsan\",\"email\":\"zhangsan@qq.com\"},{\"name\":\"lisi\",\"email\":\"lisi@163.com\"}]"; 78 79 JSONArray array = JSON.parseArray(jsonStr); 80 81 for(int i=0;i<array.size();i++){ 82 System.out.println(array.get(i)); 83 } 84 } 85 86 public static void javaBeanToJsonStr(){ 87 User user = new User(11,"wangwu","wangwu@163.com","上海"); 88 89 String str = JSON.toJSONString(user); 90 System.out.println(str); 91 } 92 93 public static void jsonStrToBeanList(){ 94 String jsonStr = "[{\"name\":\"zhangsan\",\"email\":\"zhangsan@qq.com\"},{\"name\":\"lisi\",\"email\":\"lisi@163.com\"}]"; 95 List<User> list = JSON.parseArray(jsonStr,User.class); 96 for(User user : list){ 97 System.out.println(user); 98 } 99 } 100 101 public static void beanToJsonObject(){ 102 User user = new User(11,"wangwu","wangwu@163.com","上海"); 103 104 JSONObject jsonObject = (JSONObject) JSON.toJSON(user); 105 106 System.out.println(jsonObject); 107 108 } 109 110 public static void main(String []args){ 111 //listToJsonString(); 112 //(); 113 // stringToMap(); 114 // jsonToJsonObject(); 115 // jsonStrToJsonBean(); 116 //jsonStrToJsonArray(); 117 // javaBeanToJsonStr(); 118 //jsonStrToBeanList(); 119 beanToJsonObject(); 120 121 } 122 }
附錄02:
1 public class TestOrgJson { 2 3 public static void beanToJSONObject(){ 4 User user = new User(11,"wangwu","wangwu@163.com","上海"); 5 6 JSONObject jsonObject = new JSONObject(user); 7 8 System.out.println(jsonObject); 9 } 10 11 public static void mapToJSONObject(){ 12 13 Map<String,Object> map = new HashMap(); 14 map.put("name","zhangsan"); 15 map.put("age","15"); 16 17 JSONObject jsonObject = new JSONObject(); 18 jsonObject.toMap(); 19 } 20 21 22 public static void main(String []args){ 23 24 25 26 } 27 }