zoukankan      html  css  js  c++  java
  • 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 ]
     
  • 相关阅读:
    Linux-05安装python3,jupyter(朱皮特)
    Linux-04Vim
    calloc()的使用
    根目录挂载位置错误记录
    arm裸机通过uboot运行hello world程序测试结果
    编译Uboot——错误记录
    将make的输出重定向到文件(转)
    Linux下JDK+Eclipse安装
    使用gdb+core查看错误信息
    Ubuntu下安装tftp
  • 原文地址:https://www.cnblogs.com/bigjor/p/11759913.html
Copyright © 2011-2022 走看看