Jfinal Json测试

2015年7月13日 由 Creater 留言 »

首先定义一个实体类User用于测试,还有一个Info类。

package com.demo.bean;

public class Info {
	public int id;
	public String addr;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getAddr() {
		return addr;
	}
	public void setAddr(String addr) {
		this.addr = addr;
	}
	public String getSchool() {
		return school;
	}
	public void setSchool(String school) {
		this.school = school;
	}
	public String school;
}
package com.demo.bean;

import java.util.ArrayList;
import java.util.List;

public class User {

	private static final long serialVersionUID = 1L; 
	  public int id;
	  public String name;
	  public String title;
	  public List<Info> info = new ArrayList<Info>();

	public List<Info> getInfo() {
		return info;
	}
	public void setInfo(List<Info> info) {
		this.info = info;
	}
	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 String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
}

在IndexController中展开测试:
1.

		user.id = 1;
		user.name = "Creater";//or set
		user.title = "Love Linux";
		user.info.setAddr("四川绵阳");
		user.info.setId(2);
		user.info.setSchool("SWUST");
		setAttr("user",user);
		renderJson();

测试结果:

{"user":{"id":1,"title":"Love Linux","name":"Creater","info":{"id":2,"school":"SWUST","addr":"四川绵阳"}}}

2.

		User user = new User();
		user.id = 1;
		user.name = "Creater";//or set
		user.title = "Love Linux";
		user.info.setAddr("四川绵阳");
		user.info.setId(2);
		user.info.setSchool("SWUST");
		setAttr("user",user);
		
		User user1 = new User();
		user1.id = 1;
		user1.name = "Creater1";//or set
		user1.title = "Love Linux1";
		user1.info.setAddr("四川绵阳1");
		user1.info.setId(2);
		user1.info.setSchool("SWUST1");
		setAttr("user1",user1);
		renderJson();

测试结果:

{"user1":{"id":1,"title":"Love Linux1","name":"Creater1","info":{"id":2,"school":"SWUST1","addr":"四川绵阳1"}},"user":{"id":1,"title":"Love Linux","name":"Creater","info":{"id":2,"school":"SWUST","addr":"四川绵阳"}}}

3.

		setAttr("user","creater");
		setAttr("name","renzhenwen");
		renderJson(new String[]{"user","name"});

测试结果:

{"name":"renzhenwen","user":"creater"}

4.

		user.id = 1;
		user.name = "Creater";//or set
		user.title = "Love Linux";
		user.info.setAddr("四川绵阳");
		user.info.setId(2);
		user.info.setSchool("SWUST");
		renderJson(user);

测试结果:

{"id":1,"title":"Love Linux","name":"Creater","info":{"id":2,"school":"SWUST","addr":"四川绵阳"}}

4.

renderJson("{\"age\":18} ");

测试结果:

{"age":18} 

4.

User user = new User();
		
		user.id = 1;
		user.name = "Creater";//or set
		user.title = "Love Linux";
		for(int i = 0; i < 3; ++i){
			Info info = new Info();
			info.setAddr("四川绵阳"+i);
			info.setId(i);
			info.setSchool("SWUST"+i);
			user.info.add(info);
		}
		setAttr("user",user);
		renderJson();

测试结果:

{"user":{"id":1,"title":"Love Linux","name":"Creater","info":[{"id":0,"school":"SWUST0","addr":"四川绵阳0"},{"id":1,"school":"SWUST1","addr":"四川绵阳1"},{"id":2,"school":"SWUST2","addr":"四川绵阳2"}]}}

5.

		User user = new User();
		
		user.id = 1;
		user.name = "Creater";//or set
		user.title = "Love Linux";
		for(int i = 0; i < 3; ++i){
			Info info = new Info();
			info.setAddr("四川绵阳"+i);
			info.setId(i);
			info.setSchool("SWUST"+i);
			user.info.add(info);
		}
		renderJson("test",user);

测试结果:

{"test":{"id":1,"title":"Love Linux","name":"Creater","info":[{"id":0,"school":"SWUST0","addr":"四川绵阳0"},{"id":1,"school":"SWUST1","addr":"四川绵阳1"},{"id":2,"school":"SWUST2","addr":"四川绵阳2"}]}}

jfinal_demo

广告位

发表评论

你必须 登陆 方可发表评论.