自己开发过程中使用gson和fastjson来解析json的一点总结。
一、使用谷歌的gson和阿里巴巴的fastjson都能够很好地实现json和java对象的互相转换,只是所引用的jar库不同,具体使用方法也不同。
(1)gson需要引用google的的jar,比如gson-2.3.jar(http://maven.outofmemory.cn/com.google.code.gson/gson/2.3/)
(2)fastjson需要引用alibaba的jar,比如fastjson-1.2.28.jar(http://mvnrepository.com/artifact/com.alibaba/fastjson/1.2.28)
二、测试代码
注:两种方法使用时,对象模型字段注解有差别
(1)测试main方法
package mytest; import java.lang.reflect.Type; import java.util.List; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import mytest.model.GsonModelP; import mytest.model.ModelP; /** * json - java 转换 * * @author Administrator * @time 2018-09-06 */ public class JsonParse { public static void main(String[] args) { //1、json字符串数组转java对象集合 String arrayJson = "[{" + "\"layerId\":" + "0" + ",\"layerName\":"+ "\"SIP_BDC_QJ.dbo.BDC_ZD工业地块\"" + ",\"displayFieldName\":"+ "\"YSDM\"" + ",\"value\":"+ "\"73\"" + ",\"attributes\":" + "{" + "\"地块号\":"+ "\"23066\"" + ",\"ID\":"+ "\"73\"" + ",\"ZDMJ\":"+ "\"20063.45\"" + ",\"QLRMC\":"+ "\"ABC科技有限公司\"" + ",\"SHAPE.STLength()\":"+ "\"625.749399\"" + "}" + "}]"; //(1.1)阿里巴巴JSON List<ModelP> modelPList = JSON.parseArray(arrayJson, ModelP.class); System.out.println("array JSON:" + modelPList.get(0).toString()); //(1.2)谷歌GSON Type type = new TypeToken<List<GsonModelP>>(){}.getType(); //复杂对象,定义转换类型 List<GsonModelP> modelPList2 = new Gson().fromJson(arrayJson, type); System.out.println("array GSON:" + modelPList2.get(0)); //2、json字符串转java对象 String strJson = "{" + "\"layerId\":" + "0" + ",\"layerName\":"+ "\"SIP_BDC_QJ.dbo.BDC_ZD工业宗地\"" + ",\"displayFieldName\":"+ "\"YSDM\"" + ",\"value\":"+ "\"73\"" + ",\"attributes\":" + "{" + "\"宗地号\":"+ "\"23066\"" + ",\"ID\":"+ "\"73\"" + ",\"ZDMJ\":"+ "\"20063.45\"" + ",\"QLRMC\":"+ "\"苏州晶能科技有限公司\"" + ",\"SHAPE.STLength()\":"+ "\"625.749399\"" + "}" + "}"; //(2.1)阿里巴巴JSON JSONObject jsonObj = JSON.parseObject(strJson); ModelP modelP = jsonObj.toJavaObject(ModelP.class); System.out.println("str JSON:" + modelP.toString()); //(2.2)谷歌GSON GsonModelP modelP2 = new Gson().fromJson(strJson, GsonModelP.class); System.out.println("str GSON:" + modelP2.toString()); //3、java对象转json字符串 //(3.1)阿里巴巴JSON String javaObjToJsonStr = JSON.toJSON(modelP).toString(); System.out.println("java obj to JSON:" + javaObjToJsonStr); //(3.2)谷歌GSON String javaToJson = new Gson().toJson(modelP2); System.out.println("java obj to GSON:" + javaToJson); } }
(2)测试model
package mytest.model; public class ModelP { private String layerId; private String layerName; private String displayFieldName; private String value; private ModelC attributes; public String getLayerId() { return layerId; } public void setLayerId(String layerId) { this.layerId = layerId; } public String getLayerName() { return layerName; } public void setLayerName(String layerName) { this.layerName = layerName; } public String getDisplayFieldName() { return displayFieldName; } public void setDisplayFieldName(String displayFieldName) { this.displayFieldName = displayFieldName; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } public ModelC getAttributes() { return attributes; } public void setAttributes(ModelC attributes) { this.attributes = attributes; } @Override public String toString() { return "ModelP [" + "layerId=" + layerId + ", layerName=" + layerName + ", displayFieldName=" + displayFieldName + ", value=" + value + ", attributes [" + "ID =" + attributes.getID() + ", landID =" + attributes.getLandID() + ", landArea =" + attributes.getLandArea() + ", obliName =" + attributes.getObliName() + "]" + "]"; } }
package mytest.model; import com.alibaba.fastjson.annotation.JSONField; public class ModelC { private String ID; @JSONField(name = "地块号") private String landID; @JSONField(name = "ZDMJ") private String landArea; @JSONField(name = "QLRMC") private String obliName; public String getID() { return ID; } public void setID(String iD) { ID = iD; } public String getLandID() { return landID; } public void setLandID(String landID) { this.landID = landID; } public String getLandArea() { return landArea; } public void setLandArea(String landArea) { this.landArea = landArea; } public String getObliName() { return obliName; } public void setObliName(String obliName) { this.obliName = obliName; } }
package mytest.model; public class GsonModelP { private String layerId; private String layerName; private String displayFieldName; private String value; private GsonModelC attributes; public String getLayerId() { return layerId; } public void setLayerId(String layerId) { this.layerId = layerId; } public String getLayerName() { return layerName; } public void setLayerName(String layerName) { this.layerName = layerName; } public String getDisplayFieldName() { return displayFieldName; } public void setDisplayFieldName(String displayFieldName) { this.displayFieldName = displayFieldName; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } public GsonModelC getAttributes() { return attributes; } public void setAttributes(GsonModelC attributes) { this.attributes = attributes; } @Override public String toString() { return "ModelP [" + "layerId=" + layerId + ", layerName=" + layerName + ", displayFieldName=" + displayFieldName + ", value=" + value + ", attributes [" + "ID =" + attributes.getID() + ", landID =" + attributes.getLandID() + ", landArea =" + attributes.getLandArea() + ", obliName =" + attributes.getObliName() + "]" + "]"; } }
package mytest.model; import com.google.gson.annotations.SerializedName; public class GsonModelC { private String ID; @SerializedName("地块号") private String landID; @SerializedName("ZDMJ") private String landArea; @SerializedName("QLRMC") private String obliName; public String getID() { return ID; } public void setID(String iD) { ID = iD; } public String getLandID() { return landID; } public void setLandID(String landID) { this.landID = landID; } public String getLandArea() { return landArea; } public void setLandArea(String landArea) { this.landArea = landArea; } public String getObliName() { return obliName; } public void setObliName(String obliName) { this.obliName = obliName; } }