fastjson 使用方法

前端之家收集整理的这篇文章主要介绍了fastjson 使用方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
一.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。

 
 
二.下面是自己写的一个工具类
1.先说下自己后台返回的json数据格式:
{"data":{"name":"吴江","state":"pass","personalIntroduction":"内科主治医师,毕业于广州医学院。","userName":"13687832930","gender":"男","city":"广州","primaryField":"擅长你内科类疾病治疗。","title":"主治医师","departmentTel":"020125689","department":"内科","hospital":"广州人民医院","userId":"06da18e65c1552e54778ec5b6ee4b41f","userFace":"http://192.169.12.94:8080/cmp/resources/images/face/0f21031469434309871.png","professionCertification":"http://192.169.12.94:8080/cmp/resources/images/certification/01d81d1469088888481.png","id":"01d81d0a3dbab77505d990061a07b9f7"},"errMsg":"","errCode":"0000"}

2.代码调用
	Result result=(Result)JSON.parSEObject(resultJson.toString(),Result.class);
	if (result.getErrCode().trim().equals("010007")) {
		//error_ts.setVisibility(TextView.VISIBLE);
		return;
	}
	if (result.getErrCode().trim().equals("0000")) {		
		doctorInfromation=result.getResultDataObject(DoctorInfromation.class);
	}
}
说明:调用getResultDataObject()把json data部分数据转换为DoctorInfromation实例。

3.工具类代码实现:
package com.youtebao.util;
import java.util.List;
import com.alibaba.fastjson.JSON;
public class Result {
	private String errCode;// 是否成功标示
	private String errMsg;// 描述
	private String data;
	/*public Result(String result)
	{
		Result object=(Result)JSON.parSEObject(result,Result.class);
		if(object!=null)
		{
			errCode=object.errCode;
			errMsg=object.errMsg;
			data=object.data;
		}
	}*/
	
	/**
	 * 返回"0000"成功
	 *
	 * @return
	 */
	public boolean isSuccess() {
		if ("0000".equals(errCode) ) {
			return true;
		return false;
	@SuppressWarnings("unchecked")
 public <T> T getResultDataObject(Class<?> parserClass) {
 if (data != null) {
 return (T) JSON.parSEObject(data,parserClass);
 }
 return null;
 }
	@SuppressWarnings("unchecked")
	public <T> List<T> getResultDataList(Class<?> parserClass) {
 if (data != null) {
 return (List<T>) JSON.parseArray(data,parserClass);
 }
 return null;
 }
 public String getErrMsg() {
 return errMsg;
 }
 public void setErrMsg(String errMsg) {
 this.errMsg = errMsg;
 }
 public String getErrCode() {
 return errCode;
 }
 public void setErrCode(String errCode) {
 this.errCode = errCode;
 }
 public String getData() {
 return data;
 }
 public void setData(String data) {
 this.data = data;
 }
}
说明:调用getResultDataObject返回对象,调用getResultDataList返回列表。
 
 
 
4.DoctorInfromation 类
package com.youtebao.entity;
import java.io.Serializable;
public class DoctorInfromation implements Serializable{
  /**
	 *
	 */
	private static final long serialVersionUID = -908860496548994170L;
	public static final String TYPE_BASE = "base";
  public static final String TYPE_INTRODUCE = "introduce";
  public static final String TYPE_APPROVE = "approve";
 
  public static final String STATE_UN_CHECK = "uncheck";
  public static final String STATE_UN_PASS = "unpass";
  public static final String STATE_PASS = "pass";
 
  private String userId;
	private String userName;
	private String userFace;
	
	private String doctorId;
	private String name;
	private String gender;
	private String city;
	private String hospital;
	private String department;
	private String title;
	
	private String personalIntroduction;
	private String primaryField;
	
	private String departmentTel;
	private String professionCertification;
	
	private String state;
	
	public void setName(String name)
	{
		this.name=name;
	}
	
	public String getName()
	{
		return name;
	}
	
	public void setGender(String gender)
	{
		this.gender=gender;
	}
	
	public String getGender()
	{
		return gender;
	}
	public void setCity(String city)
	{
		this.city=city;
	}
	
	public String getCity()
	{
		return city;
	}
	public void setHospital(String hospital)
	{
		this.hospital=hospital;
	}
	
	public String getHospital()
	{
		return hospital;
	}
	public void setDepartment(String department)
	{
		this.department=department;
	}
	
	public String getDepartment()
	{
		return department;
	}
	public void setTitle(String title)
	{
		this.title=title;
	}
	
	public String getTitle()
	{
		return title;
	}
	
	public void setPersonalIntroduction(String personalIntroduction)
	{
		this.personalIntroduction=personalIntroduction;
	}
	
	public String getPersonalIntroduction()
	{
		return personalIntroduction;
	}
	public void setPrimaryField(String primaryField)
	{
		this.primaryField=primaryField;
	}
	
	public String getPrimaryField()
	{
		return primaryField;
	}
	public void setDepartmentTel(String departmentTel)
	{
		this.departmentTel=departmentTel;
	}
	
	public String getDepartmentTel()
	{
		return departmentTel;
	}
	
	public void setProfessionCertification(String professionCertification)
	{
		this.professionCertification=professionCertification;
	}
	
	public String getProfessionCertification()
	{
		return professionCertification;
	}
	
	public void setState(String state)
	{
		this.state=state;
	}
	
	public String getState()
	{
		return state;
	}


	public String getUserId() {
		return userId;
	}


	public void setUserId(String userId) {
		this.userId = userId;
	}


	public String getUserName() {
		return userName;
	}


	public void setUserName(String userName) {
		this.userName = userName;
	}


	public String getUserFace() {
		return userFace;
	}


	public void setUserFace(String userFace) {
		this.userFace = userFace;
	}


	public String getDoctorId() {
		return doctorId;
	}

	public void setDoctorId(String doctorId) {
		this.doctorId = doctorId;
	}


	
}
说明:DoctorInfromation 类与上面的json data部分的数据对应
原文链接:https://www.f2er.com/json/289269.html

猜你在找的Json相关文章