常用三种json包对比使用

工作中常用的JSON包

fastjson.jar 阿里巴巴的json包,强调转换性能。
json-lib.jar net.sf.json的json包,转换功能一般。
gson.jar google的json包,转换功能强大,即使内部类类型也行。

一、fastjson.jar

1.pom.xml
   <!-- 阿里巴巴json包 -->
	<dependency>
	    <groupId>com.alibaba</groupId>
	    <artifactId>fastjson</artifactId>
	    <version>1.2.70</version>
	</dependency>
2.json字符串
package com.ykq.constant;

public class Constant {
	public static final String JSON_STR
		= "{\r\n" + 
				"	\"success\": true,\r\n" + 
				"	\"respCode\": 0,\r\n" + 
				"	\"respMsg\": \"成功\",\r\n" + 
				"	\"respData\": [{\r\n" + 
				"		\"message\": \"135000000000\",\r\n" + 
				"		\"saveState\": true,\r\n" + 
				"		\"seqNo\": 0\r\n" + 
				"	}, {\r\n" + 
				"		\"message\": \"13500000001\",\r\n" + 
				"		\"saveState\": true,\r\n" + 
				"		\"seqNo\": 1\r\n" + 
				"	}]\r\n" + 
				"}";
}
3.bean对象
package com.ykq.bean;

public class RespBean {
	private boolean success;
	private int respCode;
	private String respMsg;
	private RespData[] respDataArr;
	
	/**
	 * 成员内部类.
	 * @author YKQ
	 *
	 */
	public class RespData{
		public String message;
		public boolean saveState;
		public int seqNo;
		public String getMessage() {
			return message;
		}
		public void setMessage(String message) {
			this.message = message;
		}
		public boolean isSaveState() {
			return saveState;
		}
		public void setSaveState(boolean saveState) {
			this.saveState = saveState;
		}
		public int getSeqNo() {
			return seqNo;
		}
		public void setSeqNo(int seqNo) {
			this.seqNo = seqNo;
		}
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public int getRespCode() {
		return respCode;
	}

	public void setRespCode(int respCode) {
		this.respCode = respCode;
	}

	public String getRespMsg() {
		return respMsg;
	}

	public void setRespMsg(String respMsg) {
		this.respMsg = respMsg;
	}

	public RespData[] getRespDataArr() {
		return respDataArr;
	}

	public void setRespDataArr(RespData[] respDataArr) {
		this.respDataArr = respDataArr;
	}
}
4.fastjson.jar使用
package com.ykq.json;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ykq.bean.RespBean;
import com.ykq.bean.RespBean.RespData;
import com.ykq.constant.Constant;

/**
 * @author YKQ
 * 使用阿里巴巴的json包-fastjson
 * 将json字符串转换为bean对象
 * 将bean对象转换成json字符串
 */
public class JSONInAlibaba {

	public static void main(String[] args) {
		String jsonStr = Constant.JSON_STR;
		RespBean respBean = new RespBean();
		// Exception in thread "main" com.alibaba.fastjson.JSONException: can't create non-static inner class instance.
		// 通过这种方式转换json对象,对类数组类型存在问题,会报上述错误
		// 将内部类加上static能解决
		// respBean = JSONObject.parseObject(jsonStr, RespBean.class);
		// 将字符串转为jsonObject
		JSONObject jsonObject = JSONObject.parseObject(jsonStr);
		// 得到字符串
		String respMsg = jsonObject.getString("respMsg");
		respBean.setRespMsg(respMsg);
		// 得到布尔值
		boolean success = jsonObject.getBooleanValue("success");
		respBean.setSuccess(success);
		// 得到int值
		int respCode = jsonObject.getIntValue("respCode");
		respBean.setRespCode(respCode);
		// 得到数组
		JSONArray jsonArr = jsonObject.getJSONArray("respData");
		RespData[] respData = new RespData[jsonArr.size()];
		String message = null;
		boolean saveState = true;
		int seqNo = 0;
		// 遍历数组
		for(int i=0; i<jsonArr.size(); i++) {
			respData[i] = respBean.new RespData();
			JSONObject arrObject = (JSONObject) jsonArr.get(i);
			message = arrObject.getString("message");
			respData[i].setMessage(message);
			saveState = arrObject.getBooleanValue("saveState");
			respData[i].setSaveState(saveState);
			seqNo = arrObject.getIntValue("seqNo");
			respData[i].setSeqNo(seqNo);
		}
		respBean.setRespDataArr(respData);
		// 将respBean转换为json字符串
		String resp = JSONObject.toJSONString(respBean);
		System.out.println(resp);
	}
}

二、json-lib.jar

1.pom.xml
	<!-- net.sfjson包 -->
	<dependency>
	    <groupId>net.sf.json-lib</groupId>
	    <artifactId>json-lib</artifactId>
	    <version>2.4</version>
	    <!-- Missing artifact net.sf.json-lib:json-lib:jar:2.4 -->
	    <classifier>jdk15</classifier>
	</dependency>
2.net.sf.json-lib.jar的使用
package com.ykq.json;

import com.ykq.bean.RespBean;
import com.ykq.bean.RespBean.RespData;
import com.ykq.constant.Constant;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @author YKQ
 * 使用net.sf.json
 * 转换过程中都是需要先获取JSONObject
 * 将字符串转换为bean
 * 将bean转换为json
 */
public class JSONInSfNet {

	public static void main(String[] args) {
		String jsonStr = Constant.JSON_STR;
		JSONObject jsonObject = JSONObject.fromObject(jsonStr);
		// 直接转换存在问题,因为RespBean包含内部类,普通的类,是可以转换的
		// RespBean respBean = (RespBean) JSONObject.toBean(jsonObject);
		RespBean respBean = new RespBean();
		boolean success = jsonObject.getBoolean("success");
		respBean.setSuccess(success);
		int respCode = jsonObject.getInt("respCode");
		respBean.setRespCode(respCode);
		String respMsg = jsonObject.getString("respMsg");
		respBean.setRespMsg(respMsg);
		JSONArray jsonArr = jsonObject.getJSONArray("respData");
		RespData[] respDataArr = new RespData[jsonArr.size()];
		RespData respData = null;
		JSONObject jsonObject0 = null;
		String message = null;
		boolean saveState = true;
		int seqNo = 0;
		for(int i=0; i<jsonArr.size(); i++) {
			respData = respBean.new RespData();
			jsonObject0 = jsonArr.getJSONObject(i);
			message = jsonObject0.getString("message");
			respData.setMessage(message);
			saveState = jsonObject0.getBoolean("saveState");
			respData.setSaveState(saveState);
			seqNo = jsonObject0.getInt("seqNo");
			respData.setSeqNo(seqNo);
			respDataArr[i] = respData;
		}
		respBean.setRespDataArr(respDataArr);
		// 将bean转换为string
		JSONObject jsonObject1 = JSONObject.fromObject(respBean);
		String json = jsonObject1.toString();
		System.out.println(json);
	}
}

三、gson.jar

1.pom.xml
	<!-- google的json包 命名gson -->
	<dependency>
	    <groupId>com.google.code.gson</groupId>
	    <artifactId>gson</artifactId>
	    <version>2.8.6</version>
	</dependency>
2.gson.jar的使用
package com.ykq.json;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.ykq.bean.RespBean;
import com.ykq.constant.Constant;

/**
 * @author YKQ
 * 使用google的json包,在字符串转换为bean时先转换为JsonElement,再转换为bean,可以快速实现。
 * 将字符串转换为bean
 * 将bean转换为字符串
 */
public class JSONInGoogle {

	public static void main(String[] args) {
		String jsonStr = Constant.JSON_STR;
		// google的json包命名gson,还要实例化
		Gson json = new Gson();
		// 这种转换方式在类数组中有问题
		// RespBean respBean = json.fromJson(jsonStr, RespBean.class);
		// 需要先经过JsonElement类型,再转换
		JsonElement jsonElement = JsonParser.parseString(jsonStr);
		RespBean respBean = json.fromJson(jsonElement, RespBean.class);
		// 将bean转换为string
		System.out.println(json.toJson(respBean));
		System.out.println(json.toJson(jsonElement));
	}
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JSONObject必的Jarjson生成的简单案例 所有commons的网址: http://commons.apache.org/index.html 组装和解析JSONObject的Json字符串,共需要下面六个: 1、json-lib 2、commons-beanutils 3、commons-collections 4、commons-lang 5、commons-logging 6、ezmorph 第零个json-lib-2.4-jdk15.jar http://sourceforge.net/projects/json-lib/files/json-lib/json-lib-2.4/ 下载地址:http://nchc.dl.sourceforge.net/project/json-lib/json-lib/json-lib-2.4/json-lib-2.4-jdk15.jar 第一个: commons-beanutils-1.9.2.jar http://commons.apache.org/proper/commons-beanutils/download_beanutils.cgi 下载地址:http://mirrors.cnnic.cn/apache//commons/beanutils/binaries/commons-beanutils-1.9.2-bin.zip 第二个: (注:此不可用,改用旧) commons-collections4-4.0.jar http://commons.apache.org/proper/commons-collections/download_collections.cgi 下载地址:http://apache.dataguru.cn//commons/collections/binaries/commons-collections4-4.0-bin.zip (注:此可用,低版本的稳定性更高) commons-collections-3.2.1.jar http://commons.apache.org/proper/commons-collections/download_collections.cgi 下载地址:http://mirrors.hust.edu.cn/apache//commons/collections/binaries/commons-collections-3.2.1-bin.zip 第三个: (注:此不可用,会造成程序出错,改用旧) commons-lang3-3.3.2.jar http://commons.apache.org/proper/commons-lang/download_lang.cgi 下载地址:http://apache.dataguru.cn//commons/lang/binaries/commons-lang3-3.3.2-bin.zip (注:此可用,低版本的稳定性更高) commons-lang-2.6-bin http://commons.apache.org/proper/commons-lang/download_lang.cgi?Preferred=http%3A%2F%2Fapache.dataguru.cn%2F 下载地址:http://apache.dataguru.cn//commons/lang/binaries/commons-lang-2.6-bin.zip 第四个: commons-logging-1.1.3.jar http://commons.apache.org/proper/commons-logging/download_logging.cgi 下载地址:http://apache.dataguru.cn//commons/logging/binaries/commons-logging-1.1.3-bin.zip 第五个: ezmorph-1.0.2.jar http://ezmorph.sourceforge.net/ http://sourceforge.net/projects/ezmorph/files/ezmorph/ 下载地址:http://nchc.dl.sourceforge.net/project/ezmorph/ezmorph/ezmorph-1.0.6/ezmorph-1.0.6.jar

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值