and roidは迅速にJson解析を実現します.

3219 ワード

Fastjson  

Fastjson   Java     JSON   。
1、  http://json.org  ,               。
2、  qiang ,  JDK     ,     JavaBean、Collection、Map、Date、Enum、  。
3、   ,        jar,      JDK 。
4、  ,  Apache License 2.0    。http://code.alibabatech.com/wiki/display/FastJSON/Home

    Fastjson?
SVN:http://code.alibabatech.com/svn/fastjson/trunk/
WIKI:http://code.alibabatech.com/wiki/display/FastJSON/Home
Issue Tracking:http://code.alibabatech.com/jira/browse/FASTJSON

      Maven,maven repository    :
<repository>
	<id>opensesame</id>
	<name>Alibaba OpenSource Repsoitory</name>
	<url>http://code.alibabatech.com/mvn/releases/</url>
	<snapshots>
		<enabled>false</enabled>
	</snapshots>
</repository>

pom.xml         :
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>fastjson</artifactId>
	<version>1.0.4</version>
</dependency>

      maven,      :
http://code.alibabatech.com/mvn/releases/com/alibaba/fastjson/1.0.4/fastjson-1.0.4.jar
http://code.alibabatech.com/mvn/releases/com/alibaba/fastjson/1.0.4/fastjson-1.0.4-sources.jar


    :
Fastjson          com.alibaba.fastjson.JSON

import com.alibaba.fastjson.JSON;

public static final Object parse(String text); //  JSON  parse JSONObject  JSONArray
public static final JSONObject parseObject(String text); //  JSON  parse JSONObject
public static final <T> T parseObject(String text, Class<T> clazz); //  JSON  parse JavaBean
public static final JSONArray parseArray(String text); //  JSON  parse JSONArray
public static final <T> List<T> parseArray(String text, Class<T> clazz); // JSON  parse JavaBean  
public static final String toJSONString(Object object); //  JavaBean    JSON  
public static final String toJSONString(Object object, boolean prettyFormat); //  JavaBean        JSON  
public static final Object toJSON(Object javaObject);  JavaBean   JSONObject  JSONArray。

    :
       User Group:
public class User {
	private Long id;
	private String name;

	public Long getId() { return id; }
	public void setId(Long id) { this.id = id; }

	public String getName() { return name; }
	public void setName(String name) { this.name = name; }
}
public class Group {
	private Long id;
	private String name;
	private List<User> users = new ArrayList<User>();

	public Long getId() { return id; }
	public void setId(Long id) { this.id = id; }

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

	public List<User> getUsers() { return users; }
	public void setUsers(List<User> users) { this.users = users; }
}

Encode    :
import com.alibaba.fastjson.JSON;

Group group = new Group();
group.setId(0L);
group.setName("admin");

User guestUser = new User();
guestUser.setId(2L);
guestUser.setName("guest");

User rootUser = new User();
rootUser.setId(3L);
rootUser.setName("root");

group.getUsers().add(guestUser);
group.getUsers().add(rootUser);

String jsonString = JSON.toJSONString(group);

System.out.println(jsonString);

Decode     :
Group group2 = JSON.parseObject(jsonString, Group.class);