• Jackson基础


    一、所需jar包:

      jackson-core-x.x.x-rc4.jar、jackson-databind-x.x.x-rc4.jar、jackson-annotations-x.x.x-rc4.jar(本例使用jackson-core-2.6.0-rc4.jar、jackson-databind-2.6.0-rc4.jar、jackson-annotations-2.6.0-rc4.jar)。

    二、解析转化:

      1、json字符串  < ------ >  js

            String str="{"name" : "peter","age" : 25,"address" : "广州","weight" : "55.5","handsome" : true}";
            
            //json字符串  ------>  json
            ObjectMapper mapper=new ObjectMapper();
            JsonNode jsonNode=mapper.readTree(str);
            
            //json  ------>json字符串
            String jsonStr=jsonNode.toString();

      2、java实体类  < ------ >  json

    public class User {
        private String name;
        private int age;
        private String address;
        private double weight;
        private boolean handsome;
        //此处省略构造方法和get、set方法
            User user=new User("peter", 20, "广州", 55.5, true);
            
            ObjectMapper mapper=new ObjectMapper();
            //java实体类 ------>  json字符串
            String jsonStr=mapper.writeValueAsString(user);
            //json字符串------>json
            JsonNode json=mapper.readTree(jsonStr);
            
            //json  ------>  java实体类
            User user2=mapper.readValue(jsonStr, User.class);

      3、List  < ------ >  json

            List<String> list=new ArrayList<String>();
            list.add("QQQQQQ");
            list.add("WWWWWW");
            list.add("EEEEEE");
            list.add("RRRRRR");
            
            ObjectMapper mapper=new ObjectMapper();
            //List  ------>  json字符串
            String jsonStr=mapper.writeValueAsString(list);
            //json字符串  ------> json
            JsonNode jsonNode=mapper.readTree(jsonStr);
    //json字符串 ------> List List<String> list2=mapper.readValue(jsonStr, List.class);

      4、Map  < ------ >  json

            Map<String, String> map=new HashMap<String, String>();
            map.put("name", "peter");
            map.put("sex", "Man");
            map.put("age", "20");
            
            ObjectMapper mapper=new ObjectMapper();
            //Map  ------>  json字符串
            String jsonStr=mapper.writeValueAsString(map);
            //json字符串  ------>  json
            JsonNode json=mapper.readTree(jsonStr);
            //json  ------>  Map
            Map<String, String> map2=mapper.readValue(json.toString(), Map.class);

      5、Set  < ------ >  json

            Set<String> set=new HashSet<String>();
            set.add("QQQQQQ");
            set.add("wwwwww");
            set.add("eeeeee");
            
            ObjectMapper mapper=new ObjectMapper();
            //Set  ------>  json字符串
            String jsonStr=mapper.writeValueAsString(set);
         //json字符串 ------> json
         JsonNode jsonNode=mapper.readTree(jsonStr);
    //json字符串 ------> Set Set<String> set2=mapper.readValue(jsonStr, Set.class);

    三、CRUD:

      1、查询数据:

            //peter
            String name=jsonNode.get("name").asText();
            //"peter"
            String name1=jsonNode.get("name").toString();

            //提取handsome(true)
            JsonNode handsome=jsonNode.path("handsome");
    int age=jsonNode.get("age").asInt(); double weight=jsonNode.get("weight").asDouble(); boolean handsome=jsonNode.get("handsome").asBoolean()
            for (int i = 0; i < jsonNode.size(); i++) {
                System.out.println(jsonNode.get(i));
            }

      2、增加数据:

            ObjectMapper mapper=new ObjectMapper();
            ObjectNode node=mapper.createObjectNode();
            //{"name":"DJ","age":18}
            node.put("name", "DJ");
            node.put("age", 18);
            
            ArrayNode arrayNode=mapper.createArrayNode();
            //["name","age"]
            arrayNode.add("name");
            arrayNode.add("age");

      3、删除数据:

            ObjectMapper mapper=new ObjectMapper();
            ObjectNode node=mapper.createObjectNode();
            
            node.put("name", "DJ");
            node.put("age", 18);
            
            node.remove("name");
            ArrayNode arrayNode=mapper.createArrayNode();
            //["name","age"]
            arrayNode.add("name");
            arrayNode.add("age");
            int index=0;
            arrayNode.remove(index);

      4、修改数据:

            ObjectMapper mapper=new ObjectMapper();
            ObjectNode node=mapper.createObjectNode();
            
            node.put("name", "DJ");
            node.put("age", 18);
            
            //重置数据
            node.put("name", "MDJ");
            ArrayNode arrayNode=mapper.createArrayNode();
            //["name","age"]
            arrayNode.add("name");
            arrayNode.add("age");
            
            for (int i = 0; i < arrayNode.size(); i++) {
                if("name".equals(arrayNode.get(i).asText())){
                    arrayNode.remove(i);
                    arrayNode.add("userName");
                }
            }

     

  • 相关阅读:
    5G扫盲
    geohash-net实现
    AI(一):概念与资讯
    AI(二):人脸识别
    geohash基本原理
    Hue
    Kylin(三): Saiku
    【FreeMarker】Spring MVC与FreeMarker整合(二)
    【FreeMarker】FreeMarker快速入门(一)
    【Linux】Jenkins以war包运行及开机启动配置(四)
  • 原文地址:https://www.cnblogs.com/zhukunqiang/p/7526971.html
Copyright © 2020-2023  润新知