天天看點

JSON的處理

08-JSON的處理-JSON的回顧

  • JSON(JavaScript Object Notation):是一種輕量級的資料交換格式。
  • 它是基于 ECMAScript 規範的一個子集,采用完全獨立于程式設計語言的文本格式來存儲和表示資料。
  • 簡潔和清晰的層次結構使得 JSON 成為理想的資料交換語言。易于人閱讀和編寫,同時也易于計算機解析和生成,并有效的 提升網絡傳輸效率。
  • 我們除了可以在 JavaScript 中來使用 JSON 以外,在 JAVA 中同樣也可以使用 JSON。
  • JSON 的轉換工具是通過 JAVA 封裝好的一些 JAR 工具包。
  • 可以将 JAVA 對象或集合轉換成 JSON 格式的字元串,也可以将 JSON 格式的字元串轉成 JAVA 對象。
  • Jackson:開源免費的 JSON 轉換工具,SpringMVC 轉換預設使用 Jackson。
      1. 導入 jar 包。
      2. 建立核心對象。
      3. 調用方法完成轉換。
  • 常用類
  • ObjectMapper常用方法

09-JSON的處理-JSON轉換工具的介紹

10-JSON的處理-JSON轉換的練習01

  1. 對象轉 JSON, JSON 轉對象。
    /*
        JSON轉換工具的使用
     */
    public class ObjectMapperTest {    
    	private ObjectMapper mapper = new ObjectMapper();
        /*
            1.User對象轉json, json轉User對象
              json字元串 = {"name":"張三","age":23}
              user對象 = User{name='張三', age=23}
         */
        @Test
        public void test01() throws Exception{
            //User對象轉json
            User user = new User("張三",23);
            String json = mapper.writeValueAsString(user);
            System.out.println("json字元串:" + json);
    
            //json轉User對象
            User user2 = mapper.readValue(json, User.class);
            System.out.println("java對象:" + user2);
        }
    }
               
  2. Map轉 JSON, JSON 轉 Map。
    /*
      2.map<String,String>轉json, json轉map<String,String>
      json字元串 = {"姓名":"張三","性别":"男"}
      map對象 = {姓名=張三, 性别=男}
    */
    @Test
    public void test02() throws Exception{
        //map<String,String>轉json
        HashMap<String,String> map = new HashMap<>();
        map.put("姓名","張三");
        map.put("性别","男");
        String json = mapper.writeValueAsString(map);
        System.out.println("json字元串:" + json);
    
        //json轉map<String,String>
        HashMap<String,String> map2 = mapper.readValue(json, HashMap.class);
        System.out.println("java對象:" + map2);
    }
               
/*
  3.map<String,User>轉json, json轉map<String,User>
    json字元串 = {"黑馬一班":{"name":"張三","age":23},"黑馬二班":{"name":"李四","age":24}}
    map對象 = {黑馬一班=User{name='張三', age=23}, 黑馬二班=User{name='李四', age=24}}
 */
@Test
public void test03() throws Exception{
    //map<String,User>轉json
    HashMap<String,User> map = new HashMap<>();
    map.put("黑馬一班",new User("張三",23));
    map.put("黑馬二班",new User("李四",24));
    String json = mapper.writeValueAsString(map);
    System.out.println("json字元串:" + json);

    //json轉map<String,User>
    HashMap<String,User> map2 = mapper.readValue(json,new TypeReference<HashMap<String,User>>(){});
    System.out.println("java對象:" + map2);
}
           
  1. List轉 JSON, JSON 轉 List。
    /*
      4.List<String>轉json, json轉 List<String>
        json字元串 = ["張三","李四"]
        list對象 = [張三, 李四]
    */
    @Test
    public void test04() throws Exception{
        //List<String>轉json
        ArrayList<String> list = new ArrayList<>();
        list.add("張三");
        list.add("李四");
        String json = mapper.writeValueAsString(list);
        System.out.println("json字元串:" + json);
    
        //json轉 List<String>
        ArrayList<String> list2 = mapper.readValue(json,ArrayList.class);
        System.out.println("java對象:" + list2);
    }
               
  2. /*
      5.List<User>轉json, json轉List<User>
        json字元串 = [{"name":"張三","age":23},{"name":"李四","age":24}]
        list對象 = [User{name='張三', age=23}, User{name='李四', age=24}]
         */
    @Test
    public void test05() throws Exception{
        //List<User>轉json
        ArrayList<User> list = new ArrayList<>();
        list.add(new User("張三",23));
        list.add(new User("李四",24));
        String json = mapper.writeValueAsString(list);
        System.out.println("json字元串:" + json);
    
        //json轉List<User>
        ArrayList<User> list2 = mapper.readValue(json,new TypeReference<ArrayList<User>>(){});
        System.out.println("java對象:" + list2);
    }
               

11-JSON的處理-JSON轉換的練習02

12-JSON的處理-JSON處理的小結

  • **Jackson:**開源免費的 JSON 轉換工具,SpringMVC 轉換預設使用 Jackson。