zoukankan      html  css  js  c++  java
  • jackson readTree

    String jsonstr = "{"msg":{"head":{"version":"1.0","bizcode":"1006","senddate":"20140827","sendtime":"110325","seqid":"1"},"body":{"datalist":"wahaha","rstcode":"000000","rstmsg":"成功"}}}";
            ObjectMapper mapper = new ObjectMapper();  
            
            //允许出现特殊字符和转义符
            mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true) ;
            JsonNode root = mapper.readTree(jsonstr); 
            
         //path与get作用相同,但是当找不到该节点的时候,返回missing node而不是Null.  JsonNode msg = root.path("msg"); JsonNode head = msg.path("head"); JsonNode body = msg.path("body"); String bizcode = head.path("bizcode").asText(); String datalist = body.path("datalist").asText(); System.err.println(bizcode); System.err.println(datalist); System.err.println(root.path("msg").path("body").path("datalist").asText());

      

    try {
    			ObjectMapper mapper = new ObjectMapper();
    
    			// 允许出现特殊字符和转义符
    			mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
    
    			// String jsonstr =
    			// "{"msg":{"head":{"version":"1.0","bizcode":"1006","senddate":"20140827","sendtime":"110325","seqid":"1"},"body":{"datalist":"wahaha","rstcode":"000000","rstmsg":"成功"}}}";
    
    			ObjectNode root = mapper.createObjectNode();
    			ObjectNode msg = mapper.createObjectNode();
    
    			ObjectNode head = mapper.createObjectNode();
    			head.put("version", "1.0");
    			head.put("bizcode", "1006");
    			head.put("senddate", "20140827");
    			head.put("sendtime", "110325");
    			head.put("seqid", "1");
    
    			ObjectNode body = mapper.createObjectNode();
    			body.put("datalist", "wahaha");
    			body.put("rstcode", "000000");
    			body.put("rstmsg", "成功");
    
    			msg.put("head", head);
    			msg.put("body", body);
    			root.put("msg", msg);
    
    			System.out.println(mapper.writeValueAsString(root));
    
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    

      

  • 相关阅读:
    牛客网-2019校招真题-跳格子游戏(斐波那契数列)
    牛客网-2019校招真题-学数学
    牛客网-2019校招真题-方格走法
    牛客网-2019年校招真题-通过率降序(二)
    牛客网-2019年校招真题-通过率降序(一)
    常用数据库连接URL的举例
    Css3-渐变
    清除浮动的方法
    html中的条件注释
    Css中的定位
  • 原文地址:https://www.cnblogs.com/yangy608/p/3939315.html
Copyright © 2011-2022 走看看