com.alibaba.fastjson.JSONObject;的使用

 

转:

com.alibaba.fastjson.JSONObject;的使用

1  POM.xml

  1. <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
  2. <dependency>
  3. <groupId>com.alibaba</groupId>
  4. <artifactId>fastjson</artifactId>
  5. <version>1.2.51</version>
  6. </dependency>

2  附上代码例子

2.1 创建2个实体类,供后面例子使用

  1. public class School {
  2. private String id;
  3. private String name;
  4. List<User> students = new ArrayList<User>();
  5. public String getId() {
  6. return id;
  7. }
  8. public void setId(String id) {
  9. this.id = id;
  10. }
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. public List<User> getStudents() {
  18. return students;
  19. }
  20. public void setStudents(List<User> students) {
  21. this.students = students;
  22. }
  23. }
  1. public class User {
  2. private String id;
  3. private String name;
  4.  
  5. public User(){
  6.  
  7. }
  8. public User(String id, String name){
  9. this.id = id;
  10. this.name = name;
  11. }
  12.  
  13. public String getId() {
  14. return id;
  15. }
  16. public void setId(String id) {
  17. this.id = id;
  18. }
  19. public String getName() {
  20. return name;
  21. }
  22. public void setName(String name) {
  23. this.name = name;
  24. }
  25. }

 2.2 Json字符串与Map、List、object之间的相互转换

  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5.  
  6. import com.alibaba.fastjson.JSON;
  7. import com.alibaba.fastjson.JSONArray;
  8. import com.alibaba.fastjson.JSONObject;
  9.  
  10. public class TestFastJson {
  11.  
  12. public static void main(String[] args){
  13. json2JsonObject();//将Json字符串转换为JSONObject对象
  14. json2JavaBean();//将Json字符串转换为JavaBean对象
  15. json2JsonArray();//将Json字符串转换为JSONArray对象
  16. json2JavaBeanList();//将Json字符串转换为JavaBean的集合
  17. javaBean2Json();//将JavaBean转换为Json格式的数据
  18. javaBean2JsonObject();//将JavaBean转换为JSONObject对象
  19. json2ListInMap();//从Json字符串的Map中获取List对象
  20. list2JsonInMap();//将含list的Map对象转换为Json字符串
  21. stringToMap();//json字符串转map
  22. mapToString();//map转json字符串
  23. mapToJsonObject();//map转json对象
  24. testList2String();//list转json字符串
  25. }
  26.  
  27. private static void json2JsonObject() {
  28. String s = "{\"name\":\"peter\"}";
  29. JSONObject object = JSON.parseObject(s);
  30. System.out.println(object.get("name"));
  31. }
  32.  
  33. private static void json2JavaBean() {
  34. String s = "{\"id\":\"17051801\",\"name\":\"lucy\"}";
  35. User user = JSON.parseObject(s, User.class);
  36. System.out.println(user.getId());
  37. System.out.println(user.getName());
  38. }
  39.  
  40. private static void json2JsonArray() {
  41. String s = "[{\"id\":\"17051801\",\"name\":\"lucy\"},{\"id\":\"17051802\",\"name\":\"peter\"}]";
  42. JSONArray array = JSON.parseArray(s);
  43. for (int i = 0; i < array.size(); i++) {
  44. //JSONArray中的数据转换为String类型需要在外边加"";不然会报出类型强转异常!
  45. String str = array.get(i)+"";
  46. JSONObject object = JSON.parseObject(str);
  47. System.out.println(object.get("name"));
  48. }
  49. }
  50.  
  51. private static void json2JavaBeanList() {
  52. String s = "[{\"id\":\"17051801\",\"name\":\"lucy\"},{\"id\":\"17051802\",\"name\":\"peter\"}]";
  53. List<User> list = JSON.parseArray(s, User.class);
  54. for (User user : list) {
  55. System.out.println(user.getName());
  56. }
  57. }
  58.  
  59. private static void javaBean2Json() {
  60. User user = new User("17051801", "lucy");
  61. String string = JSON.toJSONString(user);
  62. System.out.println(string);
  63. }
  64.  
  65. private static void javaBean2JsonObject() {
  66. User user = new User("17051801", "lucy");
  67. JSONObject json = (JSONObject) JSON.toJSON(user);
  68. System.out.println(json.get("id"));
  69. }
  70.  
  71. private static void json2ListInMap() {
  72. String s = "{json:[{id:\"17051801\",\"name\":\"lucy\"},{id:\"17051802\",\"name\":\"peter\"},"
  73. + "{id:\"17051803\",\"name\":\"tom\"},{id:\"17051804\",\"name\":\"lily\"}]}";
  74. //将Json字符串转换为JSONObject对象,并取出list对象的值
  75. JSONObject object = JSON.parseObject(s);
  76. Object objArray = object.get("json");
  77. String str = objArray+"";
  78. //方式1:转换成JSONArray对象形式
  79. JSONArray array = JSON.parseArray(str);
  80. for (int i = 0; i < array.size(); i++) {
  81. JSONObject obj = JSON.parseObject(array.get(i)+"");
  82. System.out.println(obj.get("name"));
  83. }
  84. //方式2:转换成List<JavaBean>形式
  85. List<User> list = JSON.parseArray(str, User.class);
  86. for (User user : list) {
  87. System.out.println(user.getName());
  88. }
  89. }
  90.  
  91. private static void list2JsonInMap() {
  92. //方式1:构建一个带有list的JavaBean对象
  93. School school = new School();
  94. school.setId("1");
  95. school.setName("schoolA");
  96.  
  97. User user1 = new User();
  98. user1.setId("17051801");
  99. user1.setName("lucy");
  100. User user2 = new User();
  101. user2.setId("17051802");
  102. user2.setName("peter");
  103.  
  104. school.getStudents().add(user1);
  105. school.getStudents().add(user2);
  106. //将JavaBean对象转换成Json字符串
  107. String string1 = JSON.toJSONString(school);
  108. System.out.println(string1);
  109.  
  110. //方式2:构建一个带有list的Map对象
  111. Map<String, Object> map1 = new HashMap<String,Object>();
  112. map1.put("id", "17051801");
  113. map1.put("name", "lucy");
  114.  
  115. Map<String, Object> map2 = new HashMap<String,Object>();
  116. map2.put("id", "17051802");
  117. map2.put("name", "peter");
  118.  
  119. List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
  120. list.add(map1);
  121. list.add(map2);
  122.  
  123. Map<String, Object> map = new HashMap<String,Object>();
  124. map.put("id", "1");
  125. map.put("name", "schoolA");
  126. map.put("students", list);
  127. //将map对象转换成Json字符串
  128. String string2 = JSON.toJSONString(map);
  129. System.out.println(string2);
  130. }
  131.  
  132. private static void stringToMap(){
  133. String str = "{\"age\":\"24\",\"name\":\"cool_summer_moon\"}";
  134. JSONObject jsonObject = JSONObject.parseObject(str);
  135. //json对象转Map
  136. Map<String,Object> map = (Map<String,Object>)jsonObject;
  137. System.out.println("map对象是:" + map);
  138. Object object = map.get("age");
  139. System.out.println("age的值是"+object);
  140. }
  141.  
  142. private static void mapToString(){
  143. Map<String,Object> map = new HashMap<>();
  144. map.put("age", 24);
  145. map.put("name", "cool_summer_moon");
  146. String jsonString = JSON.toJSONString(map);
  147. System.out.println("json字符串是:"+jsonString);
  148. }
  149.  
  150. private static void mapToJsonObject(){
  151. Map<String,Object> map = new HashMap<>();
  152. map.put("age", 24);
  153. map.put("name", "cool_summer_moon");
  154. JSONObject json = new JSONObject(map);
  155. System.out.println("Json对象是:" + json);
  156.  
  157. }
  158.  
  159. /**
  160. * 测试包装类型的List转换为json字符串
  161. */
  162. public static void testList2String() {
  163. List<Long> longs = new ArrayList<Long>();
  164. longs.add(1L);
  165. longs.add(2L);
  166. longs.add(3L);
  167. String actual = JSON.toJSONString(longs);
  168. Assert.assertEquals("[1,2,3]", actual);
  169. }
  170.  
  171. }
posted @ 2019-10-10 15:35  戈博折刀  阅读(2364)  评论(0编辑  收藏  举报