import java.util.ArrayList;  
import java.util.HashMap;  
import java.util.List;  
import java.util.Map;  
  
import com.alibaba.fastjson.JSON;  
import com.alibaba.fastjson.TypeReference;  
public class TestMain {

	 static class Person{          
	        private String id ;  
	        private String name;  
	        private int age ;  
	          
	        public Person(){  
	              
	        }  
	        public Person(String id,String name,int age){  
	            this.id=id;  
	            this.name=name;  
	            this.age=age;  
	        }  
	        public String getId() {  
	            return id;  
	        }  
	        public void setId(String id) {  
	            this.id = id;  
	        }  
	        public String getName() {  
	            return name;  
	        }  
	        public void setName(String name) {  
	            this.name = name;  
	        }  
	        public int getAge() {  
	            return age;  
	        }  
	        public void setAge(int age) {  
	            this.age = age;  
	        }  
	        @Override  
	        public String toString() {  
	            return "Person [age=" + age + ", id=" + id + ", name=" + name + "]";  
	        }  
	          
	          
	    }  
	    public static void main(String[] args) {  
	        method1();  
	        method2();  
	        method3();  
	        method4();  
	    }  
	      
	    static void method1(){  
	        System.out.println("JavaBean转化示例开始----------");  
	        Person person = new Person("1001","fastjson",22);  
	          
	        //这里将javabean转化成json字符串  
	        String jsonString = JSON.toJSONString(person);  
	        System.out.println(jsonString);  
	        //这里将json字符串转化成javabean对象,  
	        person =JSON.parseObject(jsonString,Person.class);  
	        System.out.println(person.toString());  

	    }  
	      
	    static void method2(){  
	        System.out.println("List转化示例开始----------");  
	          
	        Person person1 = new Person("1","fastjson1",1);  
	        Person person2 = new Person("2","fastjson2",2);  
	        List persons = new ArrayList();  
	        persons.add(person1);  
	        persons.add(person2);  
	        String jsonString = JSON.toJSONString(persons);  
	        System.out.println("json字符串:"+jsonString);  
	          
	        //解析json字符串  
	        List persons2 = JSON.parseArray(jsonString,Person.class);  
	        //输出解析后的person对象
	        System.out.println("person1对象:"+persons2.get(0).toString());  
	        System.out.println("person2对象:"+persons2.get(1).toString());  

	    }  
	      
	    static void method3(){  
	        System.out.println("List转化示例开始----------");  
	        List list = new ArrayList();  
	        list.add("fastjson1");  
	        list.add("fastjson2");  
	        list.add("fastjson3");  
	        String jsonString = JSON.toJSONString(list);  
	        System.out.println("json字符串:"+jsonString);  
	          
	        //解析json字符串  
	        List list2 = JSON.parseObject(jsonString,new TypeReference>(){});   
	        System.out.println(list2.get(0)+"::"+list2.get(1)+"::"+list2.get(2));  
	  
	    }  
	      
	    static void method4(){  
	        System.out.println(" List>转化示例开始 ----------");  
	        Map map = new HashMap();  
	        map.put("key1", "value1");  
	        map.put("key2", "value2");  
	        Map map2 = new HashMap();  
	        map2.put("key1", 1);  
	        map2.put("key2", 2);  
	        List> list = new ArrayList>();  
	        list.add(map);  
	        list.add(map2);  
	        String jsonString = JSON.toJSONString(list);  
	        System.out.println("json字符串:"+jsonString);  
	        //解析json字符串  
	        List> list2 = JSON.parseObject(jsonString,new TypeReference>>(){});  
	          
	        System.out.println("map的key1值"+list2.get(0).get("key1"));  
	        System.out.println("map的key2值"+list2.get(0).get("key2"));  
	        System.out.println("ma2p的key1值"+list2.get(1).get("key1"));  
	        System.out.println("map2的key2值"+list2.get(1).get("key2"));  
	    }  
}


输出:

JavaBean转化示例开始----------

{"age":22,"id":"1001","name":"fastjson"}

Person [age=22, id=1001, name=fastjson]

List转化示例开始----------

json字符串:[{"age":1,"id":"1","name":"fastjson1"},{"age":2,"id":"2","name":"fastjson2"}]

person1对象:Person [age=1, id=1, name=fastjson1]

person2对象:Person [age=2, id=2, name=fastjson2]

List转化示例开始----------

json字符串:["fastjson1","fastjson2","fastjson3"]

fastjson1::fastjson2::fastjson3

 List>转化示例开始 ----------

json字符串:[{"key1":"value1","key2":"value2"},{"key1":1,"key2":2}]

map的key1值value1

map的key2值value2

ma2p的key1值1

map2的key2值2