FastJSON使用案例(fastjson-1.1.28.jar)

时间:2024-10-26 17:07:50
 import java.util.List;

 import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference; public class FastJSONHelper { /**
* 将java类型的对象转换为JSON格式的字符串
* @param object java类型的对象
* @return JSON格式的字符串
*/
public static <T> String serialize(T object) {
return JSON.toJSONString(object);
} /**
* 将JSON格式的字符串转换为java类型的对象或者java数组类型的对象,不包括java集合类型
* @param json JSON格式的字符串
* @param clz java类型或者java数组类型,不包括java集合类型
* @return java类型的对象或者java数组类型的对象,不包括java集合类型的对象
*/
public static <T> T deserialize(String json, Class<T> clz) {
return JSON.parseObject(json, clz);
} /**
* 将JSON格式的字符串转换为List<T>类型的对象
* @param json JSON格式的字符串
* @param clz 指定泛型集合里面的T类型
* @return List<T>类型的对象
*/
public static <T> List<T> deserializeList(String json, Class<T> clz) {
return JSON.parseArray(json, clz);
} /**
* 将JSON格式的字符串转换成任意Java类型的对象
* @param json JSON格式的字符串
* @param type 任意Java类型
* @return 任意Java类型的对象
*/
public static <T> T deserializeAny(String json, TypeReference<T> type) {
return JSON.parseObject(json, type);
} }
 public class Person {
private int Age;
private String Name;
public int getAge() {
return Age;
}
public void setAge(int age) {
Age = age;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
}
 public class Program1 {

     /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Person person = new Person();
person.setAge(32);
person.setName("wangyunpeng");
String json = FastJSONHelper.serialize(person);
System.out.println(json); person = FastJSONHelper.deserialize(json, Person.class);
System.out.println(String.format("Name:%s,Age:%s",person.getName(),person.getAge()));
} }
 import java.util.ArrayList;

 public class Program2 {

     /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub ArrayList<Person> list = new ArrayList<Person>();
Person person1 = new Person();
person1.setAge(32);
person1.setName("wangyunpeng");
list.add(person1);
Person person2 = new Person();
person2.setAge(17);
person2.setName("shyx");
list.add(person2);
String json = FastJSONHelper.serialize(list);
System.out.println(json); Person[] persons = FastJSONHelper.deserialize(json, Person[].class);
for (Person person : persons) {
System.out.println(String.format("Name:%s,Age:%s", person.getName(),person.getAge()));
}
}
}
 import java.util.ArrayList;
import java.util.List; public class Program3 { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Person> list = new ArrayList<Person>();
Person person1 = new Person();
person1.setAge(32);
person1.setName("wangyunpeng");
list.add(person1);
Person person2 = new Person();
person2.setAge(17);
person2.setName("shyx");
list.add(person2);
String json = FastJSONHelper.serialize(list);
System.out.println(json); List<Person> personList = FastJSONHelper.deserializeList(json, Person.class);
for (Person person : personList) {
System.out.println(String.format("Name:%s,Age:%s", person.getName(),person.getAge()));
}
} }
 import java.util.ArrayList;
import java.util.List; public class Program4 { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> list = new ArrayList<String>();
list.add("wyp");
list.add("shyx");
String json = FastJSONHelper.serialize(list);
System.out.println(json);
list = FastJSONHelper.deserializeList(json, String.class);
for (String string : list) {
System.out.println(string);
}
}
}
 import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference; public class Program5 { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("key1", "value1");
map.put("key2", "value2"); HashMap<String, Object> map2 = new HashMap<String, Object>();
map2.put("key1", 1);
map2.put("key2", 2); HashMap<String, Object> map3 = new HashMap<String, Object>();
Person person1 = new Person();
person1.setAge(32);
person1.setName("wangyunpeng");
map3.put("wyp", person1);
Person person2 = new Person();
person2.setAge(17);
person2.setName("shenyunxiao");
map3.put("shyx", person2); List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
list.add(map);
list.add(map2);
list.add(map3); String json = FastJSONHelper.serialize(list);
System.out.println(json); list = FastJSONHelper.deserializeAny(json,
new TypeReference<List<HashMap<String, Object>>>() {
});
for (HashMap<String, Object> item : list) {
for (Map.Entry<String, Object> entry : item.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (value instanceof JSONObject) {
JSONObject jObj = (JSONObject) value;
String json2 = FastJSONHelper.serialize(jObj);
Person other = FastJSONHelper.deserialize(json2, Person.class);
System.out.println(String.format(
"Key:%s,Value:[Name:%s,Age:%s]", key,
other.getName(), other.getAge()));
} else {
System.out.println(String.format("Key:%s,Value:%s", key,
value));
}
}
} } }