Android利用Gson庫解析複雜結構的JSON資料
阿新 • • 發佈:2019-01-06
最近在應用Face++進行人臉識別開發時,經常需要解析Face++返回的結構複雜的JSON資料,於是便決定應用Google開發的Gson庫來減輕工作量。
首先給大家看一個比較複雜的JSON資料:
{"face":[{"attribute":{"age":{"range":5,"value":6},"gender":{"confidence":89.7809,"value":"Male"},"race":{"confidence":94.9838,"value":"White"},"smiling":{"value":83.7032}},"face_id":"ae797d3bf784abc8fa27428ec42b19f7" ,"position":{"center":{"x":33.407572,"y":33.333333},"eye_left":{"x":26.288641,"y":35.323167},"eye_right":{"x":34.399777,"y":36.374833},"height":-3.666667,"mouth_left":{"x":27.464365,"y":32.378667},"mouth_right":{"x":33.869265,"y":30.975667},"nose":{"x":27.94343,"y":33.4925},"width":17.371938},"tag":"" }],"img_height":600,"img_id":"dc69e22f73aa19d544cd237a4f5ceb15","img_width":449,"session_id":"cd4fa25b83a349a1a52f78789bcfae61","url":null,"response_code":200}
為了讓大家更清晰的看清這個層次結構,我把這個JSON資料以樹狀結構呈現給大家,如下:
{
"face": [
{
"attribute": {
"age": {
"range ": 5,
"value": 23
},
"gender": {
"confidence": 99.9999,
"value": "Female"
},
"glass": {
"confidence": 99.945,
"value": "None"
},
"pose": {
"pitch_angle": {
"value": 17
},
"roll_angle": {
"value": 0.735735
},
"yaw_angle": {
"value": -2
}
},
"race": {
"confidence": 99.6121,
"value": "Asian"
},
"smiling": {
"value": 4.86501
}
},
"face_id": "17233b4b1b51ac91e391e5afe130eb78",
"position": {
"center": {
"x": 49.4,
"y": 37.6
},
"eye_left": {
"x": 43.3692,
"y": 30.8192
},
"eye_right": {
"x": 56.5606,
"y": 30.9886
},
"height": 26.8,
"mouth_left": {
"x": 46.1326,
"y": 44.9468
},
"mouth_right": {
"x": 54.2592,
"y": 44.6282
},
"nose": {
"x": 49.9404,
"y": 38.8484
},
"width": 26.8
},
"tag": ""
}
],
"img_height": 500,
"img_id": "22fd9efc64c87e00224c33dd8718eec7",
"img_width": 500,
"session_id": "38047ad0f0b34c7e8c6efb6ba39ed355",
"url": "http://www.faceplusplus.com.cn/wp-content/themes/faceplusplus/assets/img/demo/1.jpg?v=4"
}
接下來就給大家展示如何將JSON資料轉換成我們需要的Android物件。
首先我們要定義一個JSON資料對應的POJO,即JavaBeans.java。那麼有些人可能不太熟悉JavaBeans的構建,不用擔心,只要按以下兩個原則,即可輕鬆構建:
1、[ ]前面的元素定義成List< B > b形式,如,public List< Face > face;而{ }前面的元素定義為 public C c形式即可,如,public String img_id 或 public Attribute attribute。
2、類裡面的屬性名必須跟JSON欄位裡面的Key是一模一樣的;
所有根據上述規則,我們建立出的JavaBeans.java如下:
public class JavaBeans{
private int img_height;
private int img_width;
private String img_id;
private String session_id;
private String url;
private List<Face> face; //face屬於包含多個元素的JSONArray,所以定義為List,儲存這多個元素。而且Face中包含多個屬性,故宣告為一個內部類,見下方
public int getImg_height() {
return img_height;
}
public int getImg_width() {
return img_width;
}
public String getImg_id() {
return img_id;
}
public String getSession_id() {
return session_id;
}
public String getUrl() {
return url;
}
public List<Face> getFace() {
return face;
}
public class Face {
private Attribute attribute; //Attribute中包含多個屬性,故宣告為一個內部類,見下方
private String face_id;
private Position postion;
private String tag;
public String getFace_id() {
return face_id;
}
public Attribute getAttribute() {
return attribute;
}
public Position getPostion() {
return postion;
}
public String getTag() {
return tag;
}
public class Attribute {
private Age age;
private Gender gender;
private Glass glass;
private Pose pose;
private Race race;
private Smiling smiling;
public Age getAge() {
return age;
}
public Gender getGender() {
return gender;
}
public Glass getGlass() {
return glass;
}
public Pose getPose() {
return pose;
}
public Race getRace() {
return race;
}
public Smiling getSmiling() {
return smiling;
}
public class Age {
private int range;
private int value;
public int getRange() {
return range;
}
public int getValue() {
return value;
}
}
public class Gender {
private double confidence;
private String value;
public double getConfidence() {
return confidence;
}
public String getValue() {
return value;
}
}
public class Glass {
//同Age理,省略
}
public class Pose {
//同Age理,省略
}
public class Race {
//同Age理,省略
}
public class Smiling {
//同Age理,省略
}
}
public class Position {
//同Attribute理,省略
}
}
}
最後,就是利用Gson將JSON資料轉換為JavaBeans物件了:
JSONObject result = ....; //從網站獲取的JSON資料
Gson gson = new Gson();
JavaBeans data = gson.fromJson(result.toString(), JavaBeans.class);
//獲取第一個臉譜的age值
int age=data.getFace().get(0).getAttribute().getAge().getValue();