1.json的介绍:
大家可以看看官方文档的介绍,一遍可能看的不是很明白,可以多看看,官网如下
http://www.json.org/json-zh.html
2.为什么要用json?
数据传输和交换会用到xml和json。涉及xml和json的比较,这里就不详细比较了
json的优点是:
数据格式比较简单,易于读写,格式都是压缩的,占用带宽小;
3.json操作的第三方工具
jackson,谷歌的gson,阿里的fastjson等等
4.下面主要讲谷歌的gson,要使用到gson-2.2.4.jar,引入到你的项目中,下面说下gson的API文档
在GSON的API中,提供了两个重要的方法:toJson()和fromJson()方法。其中,toJson()方法用来实现将Java对象转换为相应的JSON数据,fromJson()方法则用来实现将JSON数据转换为相应的Java对象。
4.1 toJson()方法
toJson()方法用于将Java对象转换为相应的JSON数据,主要有以下几种形式:
(1)String toJson(JsonElement jsonElement);
(2)String toJson(Object src);
(3)String toJson(Object src, Type typeOfSrc);
其中,方法(1)用于将JsonElement对象(可以是JsonObject、JsonArray等)转换成JSON数据;方法(2)用于将指定的Object对象序列化成相应的JSON数据;方法(3)用于将指定的Object对象(可以包括泛型类型)序列化成相应的JSON数据。
4.2 fromJson()方法
fromJson()方法用于将JSON数据转换为相应的Java对象,主要有以下几种形式:
(1) T fromJson(JsonElement json, Class classOfT);
(2) T fromJson(JsonElement json, Type typeOfT);
(3) T fromJson(JsonReader reader, Type typeOfT);
(4) T fromJson(Reader reader, Class classOfT);
(5) T fromJson(Reader reader, Type typeOfT);
(6) T fromJson(String json, Class classOfT);
(7) T fromJson(String json, Type typeOfT);
以上的方法用于将不同形式的JSON数据解析成Java对象。5.举例子说明,前端和后端服务器,后端服务器和第三方服务器,之间传输的数字都是字符串
代码如下:
测试类:
import java.util.List;
import com.google.gson.Gson;
public class TestDemo {
public static void main(String[] args) {
String jsonData = "{\"id\":\"23\",\"name\":\"James\", \"age\":\"33\"}";
String jsonData2 = "{'id':'23','name':'James', 'age':'33'}";
Gson gson = new Gson();
Person person = gson.fromJson(jsonData,Person.class);
System.out.println(person);
Person person2 = gson.fromJson(jsonData2,Person.class);
System.out.println(person2);
String jsonArrayData = "[{'id':'111','name':'John', 'age':'18'},{'id':'112','name':'Tom', 'age':'19'}]";
List<Person> personList= GsonUtil.parseJsonArrayWithGson(jsonArrayData, Person.class);
System.out.println(personList);
String jsonArrayDate2 = "[{\"DevKey\": \"1420\",\"DevName\": \"1号保温箱\",\"DevType\": \"0\",\"DevAddr\": \"30000086\",\"DevTempName\": \"温度(m)\",\"DevTempValue\": \"23.6\",\"DevHumiName\": \"湿度(m)\", \"DevHumiValue\": \"43.7\",\"DevStatus\": \"true\",\"DevLng\": \"0.0\",\"DevLat\": \"0.0\",\"TempStatus\": \"0\",\"HumiStatus\": \"0\",\"devDataType1\": \"0\",\"devDataType2\": \"0\",\"devPos\": \"1\"}," +
"{\"DevKey\": \"105\",\"DevName\": \"1号主机\",\"DevType\": \"0\",\"DevAddr\": \"10000043\", \"DevTempName\": \"温度(℃)\",\"DevTempValue\": \"0.0\",\"DevHumiName\": \"湿度(%RH)\", \"DevHumiValue\": \"0.0\", \"DevStatus\": \"false\",\"DevLng\": \"0.0\", \"DevLat\": \"0.0\", \"TempStatus\": \"0\", \"HumiStatus\": \"0\",\"devDataType1\": \"0\",\"devDataType2\": \"0\",\"devPos\": \"1\"}]";
List<DeviceInfo> deviceInfoList= GsonUtil.parseJsonArrayWithGson(jsonArrayDate2, DeviceInfo.class);
System.out.println(deviceInfoList);
String jsonArrayDate3 ="{'HisData': [{'DataID': '2261072','TempValue': '23.2','HumiValue': '42.3','LngValue': '0.0','LatValue': '0.0','TimeValue': '2016-05-28 07:27:59'},{'DataID': '2261193','TempValue': '23.2','HumiValue': '42.3','LngValue': '0.0','LatValue': '0.0','TimeValue': '2016-05-28 07:30:02'}]}";
YunData yunData = GsonUtil.parseJsonWithGson(jsonArrayDate3, YunData.class);
List<HisData> hisDataList = yunData.getHisData();
for(HisData list:hisDataList){
System.out.println(list);
}
}
}
json字符串要转换成java对象的类
public class Person {
private String id;
private String name;
private String age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
public class DeviceInfo {
private String DevKey;
private String DevName;
private String DevType;
private String DevAddr;
private String DevTempName;
private String DevTempValue;
private String DevHumiName;
private String DevHumiValue;
private String DevStatus;
private String DevLng;
private String DevLat;
private String TempStatus;
private String HumiStatus;
private String devDataType1;
private String devDataType2;
private String devPos;
public String getDevKey() {
return DevKey;
}
public void setDevKey(String devKey) {
DevKey = devKey;
}
public String getDevName() {
return DevName;
}
public void setDevName(String devName) {
DevName = devName;
}
public String getDevType() {
return DevType;
}
public void setDevType(String devType) {
DevType = devType;
}
public String getDevAddr() {
return DevAddr;
}
public void setDevAddr(String devAddr) {
DevAddr = devAddr;
}
public String getDevTempName() {
return DevTempName;
}
public void setDevTempName(String devTempName) {
DevTempName = devTempName;
}
public String getDevTempValue() {
return DevTempValue;
}
public void setDevTempValue(String devTempValue) {
DevTempValue = devTempValue;
}
public String getDevHumiName() {
return DevHumiName;
}
public void setDevHumiName(String devHumiName) {
DevHumiName = devHumiName;
}
public String getDevHumiValue() {
return DevHumiValue;
}
public void setDevHumiValue(String devHumiValue) {
DevHumiValue = devHumiValue;
}
public String getDevStatus() {
return DevStatus;
}
public void setDevStatus(String devStatus) {
DevStatus = devStatus;
}
public String getDevLng() {
return DevLng;
}
public void setDevLng(String devLng) {
DevLng = devLng;
}
public String getDevLat() {
return DevLat;
}
public void setDevLat(String devLat) {
DevLat = devLat;
}
public String getTempStatus() {
return TempStatus;
}
public void setTempStatus(String tempStatus) {
TempStatus = tempStatus;
}
public String getHumiStatus() {
return HumiStatus;
}
public void setHumiStatus(String humiStatus) {
HumiStatus = humiStatus;
}
public String getDevDataType1() {
return devDataType1;
}
public void setDevDataType1(String devDataType1) {
this.devDataType1 = devDataType1;
}
public String getDevDataType2() {
return devDataType2;
}
public void setDevDataType2(String devDataType2) {
this.devDataType2 = devDataType2;
}
public String getDevPos() {
return devPos;
}
public void setDevPos(String devPos) {
this.devPos = devPos;
}
@Override
public String toString() {
return "DeviceInfo [DevKey=" + DevKey + ", DevName=" + DevName
+ ", DevType=" + DevType + ", DevAddr=" + DevAddr
+ ", DevTempName=" + DevTempName + ", DevTempValue="
+ DevTempValue + ", DevHumiName=" + DevHumiName
+ ", DevHumiValue=" + DevHumiValue + ", DevStatus=" + DevStatus
+ ", DevLng=" + DevLng + ", DevLat=" + DevLat + ", TempStatus="
+ TempStatus + ", HumiStatus=" + HumiStatus + ", devDataType1="
+ devDataType1 + ", devDataType2=" + devDataType2 + ", devPos="
+ devPos + "]";
}
}
public class YunData {
private List<HisData> HisData;
public List<HisData> getHisData() {
return HisData;
}
public void setHisData(List<HisData> hisData) {
HisData = hisData;
}
@Override
public String toString() {
return "YunData [HisData=" + HisData + "]";
}
}
public class HisData {
private String DataID;
private String TempValue;
private String HumiValue;
private String LngValue;
private String LatValue;
private String TimeValue;
public String getDataID() {
return DataID;
}
public void setDataID(String dataID) {
DataID = dataID;
}
public String getTempValue() {
return TempValue;
}
public void setTempValue(String tempValue) {
TempValue = tempValue;
}
public String getHumiValue() {
return HumiValue;
}
public void setHumiValue(String humiValue) {
HumiValue = humiValue;
}
public String getLngValue() {
return LngValue;
}
public void setLngValue(String lngValue) {
LngValue = lngValue;
}
public String getLatValue() {
return LatValue;
}
public void setLatValue(String latValue) {
LatValue = latValue;
}
public String getTimeValue() {
return TimeValue;
}
public void setTimeValue(String timeValue) {
TimeValue = timeValue;
}
@Override
public String toString() {
return "HisData [DataID=" + DataID + ", TempValue=" + TempValue
+ ", HumiValue=" + HumiValue + ", LngValue=" + LngValue
+ ", LatValue=" + LatValue + ", TimeValue=" + TimeValue + "]";
}
}
7.工具类
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class GsonUtil {
// 将Json数据解析成相应的映射对象
public static <T> T parseJsonWithGson(String jsonData, Class<T> type) {
Gson gson = new Gson();
T result = gson.fromJson(jsonData, type);
return result;
}
// 将Json数组解析成相应的映射对象列表
public static <T> List<T> parseJsonArrayWithGson(String jsonData,
Class<T> type) {
Gson gson = new Gson();
List<T> result = gson.fromJson(jsonData, new TypeToken<List<T>>() {
}.getType());
return result;
}
}