前言:很早就想写这篇博客;因为懒惰,一直没发。
google 提供的Gson既可以把实体类转换为json字符串,也可以把json字符串转换为实体类。今天就介绍一下在项目中经常用的的JSON解析,摆脱逐个字段解析Json的麻烦。
制定规则
所谓制度规则,就是让服务端返回的json格式遵循一定的规则,本在项目中经常按如下格式让服务端返回json格式:{“code”:”“,”message”:”“,”data”:{}}
code: 是返回的标志;
message:是返回的提示信息。
data:返回的数据源,data才是每个接口说真正需要的数据。
用实体类来实现既定的规则
基础的实体类,包含code,message字段和动态的data(用泛型来实现动态赋值)
package com.gson.bean;
import java.io.Serializable;
public class JsonParserBase implements Serializable {
private int code;
private String message;
private T data;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
@Override
public String toString() {
return "JsonParserBase [code=" + code + ", message=" + message + ", data=" + data + "]";
}
}
普通实体类User和Area
package com.gson.bean;
public class User {
private int uId;// 用户ID
private String phone;// 账号
private String pass;// 密码
private String newPass;// 新密码
private String confirmPass; // 确认密码
private String point;// 积分
private int type;// 1是注册 2是找回密码
public String getPoint() {
return point;
}
public void setPoint(String point) {
this.point = point;
}
public int getuId() {
return uId;
}
public void setuId(int uId) {
this.uId = uId;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getNewPass() {
return newPass;
}
public void setNewPass(String newPass) {
this.newPass = newPass;
}
public String getConfirmPass() {
return confirmPass;
}
public void setConfirmPass(String confirmPass) {
this.confirmPass = confirmPass;
}
}
package com.gson.bean;
import java.io.Serializable;
public class Area implements Serializable {
private int mId;
private String mName;
private int mParentId;
private String path;
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public int getmId() {
return mId;
}
public void setmId(int mId) {
this.mId = mId;
}
public String getmName() {
return mName;
}
public void setmName(String mName) {
this.mName = mName;
}
public int getmParentId() {
return mParentId;
}
public void setmParentId(int mParentId) {
this.mParentId = mParentId;
}
}
3.模拟服务端封装json
package com.gson;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gson.bean.Area;
import com.gson.bean.JsonParserBase;
import java.lang.reflect.Type;
/**
* @desc GSON 解析的封装
* @creator caozhiqing
* @data 2015/8/20
*/
public class ParserUtil {
private static Gson gson = new Gson();
/**
* 最基础的json解析
* @param json 要解析的json字符串
* @param type 对应的类型
* @param
* @return
*/
private static T fromJson(String json,Type type){
if(json == null || "".equalsIgnoreCase(json) || type == null){
return null;
}
System.out.println("json =" + json);
if(gson == null){
gson = new Gson();
}
T returnResult = gson.fromJson(json, type);
return returnResult;
}
/**
* 专为JsonParserBase设计解析
* @param json
* @param type
* @param
* @return
*/
public static JsonParserBase fromJsonBase(String json,Type type){
JsonParserBase returnResult = fromJson(json, type);
return returnResult;
}
public static void main(String[] arg){ /*****生成json****/
Area area = new Area();
area.setmName("II");
area.setmId(11);
area.setmParentId(21);
JsonParserBase jsonParserBase = new JsonParserBase();//泛型生成Area的数据
jsonParserBase.setMessage("i am area");
jsonParserBase.setCode(200);
jsonParserBase.setData(area);
String json1 = gson.toJson(jsonParserBase);
System.out.println(json1);
User user = new User();
user.setuId(22);
user.setConfirmPass("123456");
user.setPass("123456");
user.setPhone("1344345454234");
JsonParserBase jsonParserBase1 = new JsonParserBase();//泛型生成Area的数据
jsonParserBase1.setMessage("i am user");
jsonParserBase1.setCode(200);
jsonParserBase1.setData(user);
String json2 = gson.toJson(jsonParserBase1);
System.out.println(json2);
//当字段不多,不想创建实体类,可有map构建
LinkedTreeMap map = new LinkedTreeMap();
map.put("key1","hello1");
map.put("key2","hello2");
map.put("key3", "hello3");
System.out.println(gson.toJson(map));
/*****生成json****/
/*****解析json****/
JsonParserBase areaJsonParserBase = fromJsonBase(json1, new TypeToken>() {
}.getType());
System.out.println(areaJsonParserBase.getMessage());
System.out.println(areaJsonParserBase.getData().getmName());
System.out.println(areaJsonParserBase.getData().getmId());
JsonParserBase userJsonParserBase = fromJsonBase(json2, new TypeToken>() {
}.getType());
System.out.println(userJsonParserBase.getMessage());
System.out.println(userJsonParserBase.getData().getPass());
System.out.println(userJsonParserBase.getData().getConfirmPass());
LinkedTreeMap map1 = fromJson(json3, new TypeToken>() {
}.getType());
System.out.println(map1.get("key1") + " " + map1.get("key2") + " " + map1.get("key3"));
/*****解析json****/
}
}
生成后json ;json1 = {“code”:200,”message”:”i am area”,”data”:{“mId”:11,”mName”:”II”,”mParentId”:21}}
json2 = {“code”:200,”message”:”i am user”,”data”:{“uId”:22,”phone”:”1344345454234”,”pass”:”123456”,”confirmPass”:”123456”,”type”:0}}
{“key1”:”hello1”,”key2”:”hello2”,”key3”:”hello3”}
4.解析JSON
/*****解析json****/
JsonParserBase areaJsonParserBase = fromJsonBase(json1, new TypeToken>() {
}.getType());
System.out.println(areaJsonParserBase.getMessage());
System.out.println(areaJsonParserBase.getData().getmName());
System.out.println(areaJsonParserBase.getData().getmId());
JsonParserBase userJsonParserBase = fromJsonBase(json2, new TypeToken>() {
}.getType());
System.out.println(userJsonParserBase.getMessage());
System.out.println(userJsonParserBase.getData().getPass());
System.out.println(userJsonParserBase.getData().getConfirmPass());
LinkedTreeMap map1 = fromJson(json3, new TypeToken>() {
}.getType());
System.out.println(map1.get("key1") + " " + map1.get("key2") + " " + map1.get("key3"));
/*****解析json****/
5.复杂的json解析
json都是由json数组和json对象所组成的。复杂的json无非就是多几个json对象json数组,多嵌套几成而已,下面就来做几个事例。