fastjson-一般的なデータ型とjsonの相互変換

16964 ワード

public class FastJsonTest1  
{  
  
  /** 
   *    json      
   */  
  public void array2Json(){  
    String[] arr = {"bill","green","maks","jim"};  
    String jsonText = JSON.toJSONString(arr, true);  
    System.out.println("array2Json()  :jsonText=="+jsonText);  
    //     :jsonText==["bill","green","maks","jim"]  
  }  
  
  /** 
   * json         
   */  
  public void json2Array(){  
    String jsonText = "[\"bill\",\"green\",\"maks\",\"jim\"]";  
    JSONArray jsonArr = JSON.parseArray(jsonText);  
    System.out.println("json2Array()  :jsonArr=="+jsonArr);  
    //     :jsonArr==["bill","green","maks","jim"]  
  }  
    
  /** 
   *    json      
   */  
  public void array2Json2(){  
    User user1 = new User("P001","TOM",16);  
    User user2 = new User("P002","JACKSON",21);  
    User user3 = new User("P003","MARTIN",20);  
    User[] userArr = {user1,user2,user3};  
    String jsonText = JSON.toJSONString(userArr, true);  
    System.out.println("array2Json2()  :jsonText=="+jsonText);  
    //    :jsonText==[{"age":16,"id":"P001","name":"TOM"},{"age":21,"id":"P002","name":"JACKSON"},{"age":20,"id":"P003","name":"MARTIN"}]  
  }  
    
  /** 
   * json         
   */  
  public void json2Array2(){  
    String jsonText = "[{\"age\":16,\"id\":\"P001\",\"name\":\"TOM\"},{\"age\":21,\"id\":\"P002\",\"name\":\"JACKSON\"},{\"age\":20,\"id\":\"P003\",\"name\":\"MARTIN\"}]";  
    JSONArray jsonArr = JSON.parseArray(jsonText);  
    System.out.println("json2Array2()  :jsonArr=="+jsonArr);  
    //     :jsonArr==[{"age":16,"id":"P001","name":"TOM"},{"age":21,"id":"P002","name":"JACKSON"},{"age":20,"id":"P003","name":"MARTIN"}]  
  }  
    
  /** 
   * list   json      
   */  
  public void list2Json(){  
    List list = new ArrayList();  
    User user1 = new User("L001","TOM",16);  
    list.add(user1);  
    User user2 = new User("L002","JACKSON",21);  
    list.add(user2);  
    User user3 = new User("L003","MARTIN",20);  
    list.add(user3);  
    String jsonText = JSON.toJSONString(list, true);  
    System.out.println("list2Json()  :jsonText=="+jsonText);  
    //    :jsonText==[{"age":16,"id":"L001","name":"TOM"},{"age":21,"id":"L002","name":"JACKSON"},{"age":20,"id":"L003","name":"MARTIN"}]  
  }  
    
  /** 
   * list   json      
   */  
  public void list2Json2(){  
    List list = new ArrayList();  
    Address address1 = new Address("   ","   ","    ","580053");  
    User user1 = new User("L001","TOM",16,address1);  
    list.add(user1);  
    Address address2 = new Address("   ","   ","   ","330004");  
    User user2 = new User("L002","JACKSON",21,address2);  
    list.add(user2);  
    Address address3 = new Address("   ","   ","    ","710114");  
    User user3 = new User("L003","MARTIN",20,address3);  
    list.add(user3);  
    String jsonText = JSON.toJSONString(list, true);  
    System.out.println("list2Json2()  :jsonText=="+jsonText);  
    //    :jsonText==[{"address":{"city":"   ","post":"580053","province":"   ","street":"    "},"age":16,"id":"L001","name":"TOM"},{"address":{"city":"   ","post":"330004","province":"   ","street":"   "},"age":21,"id":"L002","name":"JACKSON"},{"address":{"city":"   ","post":"710114","province":"   ","street":"    "},"age":20,"id":"L003","name":"MARTIN"}]  
  }  
  
  /** 
   * map json      
   */  
  public void map2Json(){  
    Map map = new HashMap();  
    Address address1 = new Address("   ","   ","    ","580053");  
    map.put("address1", address1);  
    Address address2 = new Address("   ","   ","   ","330004");  
    map.put("address2", address2);  
    Address address3 = new Address("   ","   ","    ","710114");  
    map.put("address3", address3);  
    String jsonText = JSON.toJSONString(map, true);  
    System.out.println("map2Json()  :jsonText=="+jsonText);  
    //    :jsonText=={"address1":{"city":"   ","post":"580053","province":"   ","street":"    "},"address2":{"city":"   ","post":"330004","province":"   ","street":"   "},"address3":{"city":"   ","post":"710114","province":"   ","street":"    "}}  
  }  
    
}