fastJSON例子2

前端之家收集整理的这篇文章主要介绍了fastJSON例子2前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package com.ylchou.testfastjson;

public class Dog {

    private int id;
    private String name;
    private int age;
    
    public int getId() {
        return id;
    }
    public void setId(int 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;
    }
}

package com.ylchou.testfastjson;

public class Student {
	private int id;
	private String name;
	private int age;
	private Dog dog;
	
	public Dog getDog() {
		return dog;
	}
	public void setDog(Dog dog) {
		this.dog = dog;
	}
	public int getId() {
		return id;
	}
	public void setId(int 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;
	}
}


package com.ylchou.testfastjson;

import com.alibaba.fastjson.JSON;

public class TestFastJSON2 {
	public static void main(String[] args) {
		Dog dog = new Dog();
		dog.setAge(1);
		dog.setId(001);
		dog.setName("luxi");
		
		Student student = new Student();
		student.setAge(16);
		student.setDog(dog);
		student.setId(007);
		student.setName("ylchou");
		Object object = JSON.toJSON(student);
		System.out.println("object1:"+object);
		
		String studentString = JSON.toJSONString(student);
		System.out.println("studentString:"+studentString);
		
		Object object2 = JSON.parSEObject(studentString);
		System.out.println("object2:"+object2);
		Object object22 = JSON.parSEObject(studentString,Student.class);
		System.out.println("object22:"+object22);
		System.out.println();
		
		
		Object object3 = JSON.parse(studentString);
		System.out.println("object3:"+object3);
		
		Object[] arrayObjects = {1,2,3,4,5,6,11,12,13,14,15,16};
		String arrayJson = JSON.toJSONString(arrayObjects);
		Object object4 = JSON.parseArray(arrayJson);
		System.out.println("object4:"+object4);
		Object object5 = JSON.parse(arrayJson);
		System.out.println("object5:"+object5);
		
	}
	

}

控制台输出

object1:{"age":16,"dog":{"age":1,"id":1,"name":"luxi"},"id":7,"name":"ylchou"} studentString:{"age":16,"name":"ylchou"} object2:{"age":16,"name":"ylchou"} object22:com.ylchou.testfastjson.Student@1ea2dfe object3:{"age":16,"name":"ylchou"} object4:[1,16] object5:[1,16]

猜你在找的Json相关文章