fastJSON使用(一)——对象转换成json

fastJSON使用(一)——对象转换成json

下面是Maven的依赖包:
com.alibaba
fastjson
1.2.8
首先一个简单的程序Main.java
package fastJSON;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(123);
		String s = JSON.toJSONString("ABCDEFG");
		System.out.println(s);
		int[] intarr = new int[5];
		for (int i = 0; i < intarr.length; i++) {
			intarr[i] = i;
		}
		String intarrJSON = JSON.toJSONString(intarr);
		System.out.println(intarrJSON);
		double[] darr = {1.2,2.3,2.4,12.5,23.1};
		System.out.println(JSON.toJSON(darr));
		Map m = new HashMap();
		m.put("param1", "value1");
		m.put("param2", "value2");
		m.put("param3", "value3");
		m.put("param4", "value4");
		m.put("param5", "value5");
		System.out.println(JSON.toJSON(m));
		Map m2 = new HashMap();
		m2.put("p1", "v1");
		m2.put("p2", "v2");
		m2.put("p3", "v3");
		m2.put("p4", "v4");
		m2.put("p5", "v5");
		System.out.println(JSON.toJSON(m2));
		List> list = new ArrayList>();
		list.add(m);
		list.add(m2);
		System.out.println(JSON.toJSONString(list));
		
		Map mm = new HashMap();
		mm.put("list",list);
		mm.put("count", 2);
		System.out.println(JSON.toJSONString(mm));
		
		Student student = new Student("s1",12,new Date());
		System.out.println(JSON.toJSONString(student));
		Student student1 = new Student("s2",12,new Date());
		Student student2 = new Student("s3",12,new Date());
		Student student3 = new Student("s4",12,new Date());
		Student student4 = new Student("s5",12,new Date());
		Student student5 = new Student("s6",12,new Date());
		List studentList = new ArrayList();
		studentList.add(student);
		studentList.add(student1);
		studentList.add(student2);
		studentList.add(student3);
		studentList.add(student4);
		studentList.add(student5);
		System.out.println(JSON.toJSONString(studentList));
	}

}
Student.java
package fastJSON;

import java.util.Date;

public class Student {
	private String name;
	private int age;
	private Date date;

	public Student() {
		// TODO Auto-generated constructor stub
	}
	
	public Student(String name,int age,Date date){
		this.name = name;
		this.age = age;
		this.date = date;
	}
	
	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;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

}
这里的Student.java需要有对应的get和set,还需要有构造函数,如果那个属性没有get函数,那么解析出来的JSON串中就没有对应的属性,下面是结果。
123
"ABCDEFG"
[0,1,2,3,4]
[1.2,2.3,2.4,12.5,23.1]
{"param5":"value5","param3":"value3","param4":"value4","param1":"value1","param2":"value2"}
{"p1":"v1","p2":"v2","p3":"v3","p4":"v4","p5":"v5"}
[{"param5":"value5","param3":"value3","param4":"value4","param1":"value1","param2":"value2"},{"p1":"v1","p2":"v2","p3":"v3","p4":"v4","p5":"v5"}]
{"count":2,"list":[{"param5":"value5","param3":"value3","param4":"value4","param1":"value1","param2":"value2"},{"p1":"v1","p2":"v2","p3":"v3","p4":"v4","p5":"v5"}]}
{"age":12,"date":1465475917155,"name":"s1"}
[{"age":12,"date":1465475917155,"name":"s1"},{"age":12,"date":1465475917175,"name":"s2"},{"age":12,"date":1465475917175,"name":"s3"},{"age":12,"date":1465475917175,"name":"s4"},{"age":12,"date":1465475917175,"name":"s5"},{"age":12,"date":1465475917175,"name":"s6"}]




你可能感兴趣的:(java,maven,json)