fastjson - 之常见的数据类型与json的相互转换

前端之家收集整理的这篇文章主要介绍了fastjson - 之常见的数据类型与json的相互转换前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
public class FastJsonTest1  
{  
  
  /** 
  * 数组转json格式字符串 
  */  
  public void array2Json(){  
    String[] arr = {"bill","green",68)">"maks",68)">"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","jim"]  
  }  
    
  /** 
  * 数组转json格式字符串 
  */  
  public void array2Json2(){  
    User user1 = new User("P001",68)">"TOM",16);  
    User user2 = new User("P002",68)">"JACKSON",153)">21);  
    User user3 = new User("P003",68)">"MARTIN",153)">20);  
    User[] userArr = {user1,user2,user3};  
    String jsonText = JSON.toJSONString(userArr,68)">"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,136); font-style:italic">/** 
  * list集合转json格式字符串 
  */  
  public void list2Json(){  
    List list = new ArrayList();  
    User user1 = new User("L001",153)">16);  
    list.add(user1);  
    User user2 = new User("L002",153)">21);  
    list.add(user2);  
    User user3 = new User("L003",153)">20);  
    list.add(user3);  
    String jsonText = JSON.toJSONString(list,68)">"list2Json()方法:jsonText=="+jsonText);  
    /** 
  * list集合转json格式字符串 
  */  
  public void list2Json2(){  
    List list = new ArrayList();  
    Address address1 = new Address("广东省",68)">"深圳市",68)">"科苑南路",68)">"580053");  
    User user1 = new User(16,address1);  
    list.add(user1);  
    Address address2 = new Address("江西省",68)">"南昌市",68)">"阳明路",68)">"330004");  
    User user2 = new User(21,address2);  
    list.add(user2);  
    Address address3 = new Address("陕西省",68)">"西安市",68)">"长安南路",68)">"710114");  
    User user3 = new User(20,address3);  
    list.add(user3);  
    String jsonText = JSON.toJSONString(list,68)">"list2Json2()方法:jsonText=="+jsonText);  
    //输出结果:jsonText==[{"address":{"city":"深圳市","post":"580053","province":"广东省","street":"科苑南路"},"age":16,{"address":{"city":"南昌市","post":"330004","province":"江西省","street":"阳明路"},"age":21,{"address":{"city":"西安市","post":"710114","province":"陕西省","street":"长安南路"},"age":20,"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,68)">"map2Json()方法:jsonText=="+jsonText);  
    //输出结果:jsonText=={"address1":{"city":"深圳市","address2":{"city":"南昌市","address3":{"city":"西安市","street":"长安南路"}}  
}  
  
}

猜你在找的Json相关文章