Simple use of JSONObject of FastJSON

The json object provided by fastJson is
equivalent to Map<String, Object>



/**
 * JSONObject 相当于一个Map
 */
public class demo1 {
    
    
   static JSONObject jsonObject;

   static {
    
    
      pojo1 pojo1 = new pojo1(1, "张三");

      jsonObject = new JSONObject();
      jsonObject.put("key1", "AAA");
      jsonObject.put("key2", 11);
      jsonObject.put("key3", new Date());
      jsonObject.put("key4", pojo1);
   }

   @Test
   public void ceui1() {
    
    


      //转json ,下面两个API的效果是一样的
      String s = jsonObject.toString();
      String jsonString = jsonObject.toJSONString();
      // 输出:  {"key1":"AAA","key2":11,"key3":1567144999465,"key4":{"id":1,"name":"张三"}}
      Object key1 = jsonObject.get("key1"); //输出 AAA

      boolean key11 = jsonObject.containsKey("key1"); //是否有指定的可以 true
      boolean key111 = jsonObject.containsKey("key111");//  false

      System.out.println("key1 = " + key1);

   }

   /**
    * 内部类
    */
   public static class pojo1 {
    
    

      private int id;
      private String name;
      //*********

      public pojo1(int id, String name) {
    
    
         this.id = id;
         this.name = name;
      }

      public int getId() {
    
    
         return id;
      }

      public void setId(int id) {
    
    
         this.id = id;
      }

      public String getName() {
    
    
         return name;
      }

      public void setName(String name) {
    
    
         this.name = name;
      }
   }

}


Guess you like

Origin blog.csdn.net/qq_41489540/article/details/109069440