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);
}
/**
* json
*/
public void json2Array(){
String jsonText = "[\"bill\",\"green\",\"maks\",\"jim\"]";
JSONArray jsonArr = JSON.parseArray(jsonText);
System.out.println("json2Array() :jsonArr=="+jsonArr);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
}