package test;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class Person {
	private int id;
	private Date birthDay;

	private String name;
	private int age;

	private Person child;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Date getBirthDay() {
		return birthDay;
	}

	public void setBirthDay(Date birthDay) {
		this.birthDay = birthDay;
	}

	public String getName() {
		return name;
	}

	public Person getChild() {
		return child;
	}

	public void setChild(Person child) {
		this.child = child;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public static void main2(String[] args) {
		Gson gson = new Gson();
		// List<Person> persons = new ArrayList<Person>();
		// for (int i = 0; i < 10; i++) {
		// Person p = new Person();
		// p.setName("name" + i);
		// p.setAge(i * 5);
		// persons.add(p);
		// }
		// String str = gson.toJson(persons);
		// System.out.println(str);

		// String str = "[{'name':'name0','age':0},{'name':'name1','age':1}]";

		// Person person = gson.fromJson(str, Person.class);

		// List<Person> ps = gson.fromJson(str, new TypeToken<List<Person>>() {
		// }.getType());
		// for (int i = 0; i < ps.size(); i++) {
		// Person p = ps.get(i);
		// System.out.println(p.getName());
		// }

		Person student1 = new Person();
		student1.setId(1);
		student1.setName("李坤");
		student1.setBirthDay(new Date());

		// //////////////////////////////////////////////////////////
		System.out.println("----------简单对象之间的转化-------------");
		String s1 = gson.toJson(student1);
		System.out.println("简单Bean转化为Json===" + s1);

		// json转为简单Bean
		Person student = gson.fromJson(s1, Person.class);
		System.out.println("Json转为简单Bean===" + student);
		// 结果:
		// 简单Bean转化为Json==={"id":1,"name":"李坤","birthDay":"Jun 22, 2012 8:27:52 AM"}
		// Json转为简单Bean===Student [birthDay=Fri Jun 22 08:27:52 CST 2012, id=1,
		// name=李坤]
		// //////////////////////////////////////////////////////////

		Person student2 = new Person();
		student2.setId(2);
		student2.setName("曹贵生");
		student2.setBirthDay(new Date());

		Person student3 = new Person();
		student3.setId(3);
		student3.setName("柳波");
		student3.setBirthDay(new Date());

		List<Person> list = new ArrayList<Person>();
		list.add(student1);
		list.add(student2);
		list.add(student3);

		System.out.println("----------带泛型的List之间的转化-------------");
		// 带泛型的list转化为json
		String s2 = gson.toJson(list);
		System.out.println("带泛型的list转化为json==" + s2);

		// json转为带泛型的list
		List<Person> retList = gson.fromJson(s2, new TypeToken<List<Person>>() {
		}.getType());
		for (Person stu : retList) {
			System.out.println(stu);
		}

		System.out.println("----------111111111111-------------");
		Map<String, Object> map = new LinkedHashMap<String, Object>();
		map.put("list", list);

		String s = gson.toJson(map);
		System.out.println(s);

		System.out.println("----------222222222222-------------");
		Map<String, Object> retMap = gson.fromJson(s,
				new TypeToken<Map<String, List<Object>>>() {
				}.getType());

		for (String key : retMap.keySet()) {
			System.out.println("key:" + key + " values:" + retMap.get(key));
			if (key.equals("list")) {
				List<Person> stuList = (List<Person>) retMap.get(key);
				System.out.println(stuList);
			}
		}

	}

	public static void main(String[] args) {

	

	}
}
