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 ]
     
  • 相关阅读:
    Shell 函数
    Shell 流程控制
    Shell test 命令
    Shell echo命令
    python 类、模块、包的区别
    postgresql vacuum table
    ssh连接断开后 shell进程退出
    ubuntu 搭建 svn服务器,使用http方式访问
    如何查看apache加载了哪些模块
    maven 的使用
  • 原文地址:https://www.cnblogs.com/bigjor/p/11759913.html
Copyright © 2011-2022 走看看