工作中常用的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));
}
}