• Jackson解析json字符串入门


     https://www.yiibai.com/jackson/jackson_tree_model.html

    import java.io.IOException;
    import java.util.Iterator;
     
    import org.codehaus.jackson.JsonNode;
    import org.codehaus.jackson.JsonParseException;
    import org.codehaus.jackson.map.JsonMappingException;
    import org.codehaus.jackson.map.ObjectMapper;
     
    public class JacksonTester {
       public static void main(String args[]){
          JacksonTester tester = new JacksonTester();
          try {
             ObjectMapper mapper = new ObjectMapper();
             String jsonString = "{"name":"Mahesh Kumar", "age":21,"verified":false,"marks": [100,90,85]}";
             JsonNode rootNode = mapper.readTree(jsonString);
     
             JsonNode nameNode = rootNode.path("name");
             System.out.println("Name: "+ nameNode.getTextValue());
     
             JsonNode ageNode = rootNode.path("age");
             System.out.println("Age: " + ageNode.getIntValue());
     
             JsonNode verifiedNode = rootNode.path("verified");
             System.out.println("Verified: " + (verifiedNode.getBooleanValue() ? "Yes":"No"));
     
             JsonNode marksNode = rootNode.path("marks");
             Iterator<JsonNode> iterator = marksNode.getElements();
             System.out.print("Marks: [ ");
             while (iterator.hasNext()) {
                JsonNode marks = iterator.next();
                System.out.print(marks.getIntValue() + " "); 
             }
             System.out.println("]");
          } catch (JsonParseException e) {
             e.printStackTrace();
          } catch (JsonMappingException e) {
             e.printStackTrace();
          } catch (IOException e) {
             e.printStackTrace();
          }
       }
    }
    、、、、、、、、、输出:
    Name: Mahesh Kumar
    Age: 21
    Verified: No
    Marks: [ 100 90 85 ]
     
  • 相关阅读:
    LeetCode 338. 比特位计数
    LeetCode 208. 实现 Trie (前缀树)
    初识restful api接口
    破解 Navicat Premium 12
    ES6 Reflect的认识
    ES6 WeakMap和WeakSet的使用场景
    sublime 注释模版插件DocBlockr的使用
    js call方法的使用
    ES6 Generator的应用场景
    ES6 Symbol的应用场景
  • 原文地址:https://www.cnblogs.com/bigjor/p/11759913.html
Copyright © 2020-2023  润新知