"face":
[
{
"position":{"mouth_right":{"y":20.822055,"x":63.7805},"mouth_left":{"y":23.111303,"x":60.460333},"center":{"y":19.047619,"x":61.5},"height":12.030075,"width":8,"nose":{"y":19.914987,"x":61.0885},"eye_left":{"y":17.609398,"x":59.178833},"eye_right":{"y":15.450175,"x":62.531167}},
"tag":"",
"face_id":"d01064a564ff641afd2306a2728e78ed",
"attribute":{"age":{"range":5,"value":19},"gender":{"value":"Male","confidence":99.9959},"race":{"value":"Asian","confidence":99.7537},"smiling":{"value":56.6604}}
},
{
"position":{"mouth_right":{"y":29.306015,"x":50.818833},"mouth_left":{"y":29.402005,"x":47.587667},"center":{"y":26.942356,"x":49.25},"height":10.776942,"width":7.166667,"nose":{"y":27.601754,"x":49.2925},"eye_left":{"y":24.736967,"x":47.546333},"eye_right":{"y":24.598421,"x":50.961833}},
"tag":"",
"face_id":"b90049c48497f06aad80a962b73c0125",
"attribute":{"age":{"range":5,"value":15},"gender":{"value":"Female","confidence":99.9999},"race":{"value":"Asian","confidence":93.3415},"smiling":{"value":96.5918}}
}
],
"session_id":"71ae412122dd468397282868478eb8e1",
"img_id":"f74448028e5ecb7daad60fd99a277e49",
"response_code":200,
"img_height":633,"img_width":950,
"url":"http://img.67.com/upload/images/2014/06/30/1404100534_539746623.jpg"
}
1.JsonParser-》parse-》JsonObject -》JsonObject.get -》getAsString
strresult为以上json数据
JsonParser parser = new JsonParser();
// 使用解析器解析json数据,返回值是JsonElement,强制转化为其子类JsonObject类型
JsonObject object = (JsonObject) parser.parse(strresult);
// 使用JsonObject的get(String memeberName)方法返回JsonElement,再使用JsonElement的getAsXXX方法得到真实类型
System.out.println("url = " + object.get("url").getAsString());
2.第一级的face[ json1, json2]包含了两个json,JsonObject.getAsJsonArray("json数组名") -》JsonArray
// JSON数组
JsonArray languages = object.getAsJsonArray("face");
System.out.println(languages.size()+"json");
3、JsonArray .get(i),get下标可以获取子节点返回JsonElement ; jsonElement.getAsJsonObject(“json数组里各个json名称”)得到JsonObject , get方法得到JsonElement ,JsonElement有 各种get数据类型方法
JsonElement jsonElement=languages.get(i);
JsonObject language = jsonElement.getAsJsonObject();
JsonObject position =language.getAsJsonObject("position");
JsonObject center =position.getAsJsonObject("center");
JsonElement x =center.get("x");
JsonElement y =center.get("y");
int xi=x.getAsInt();
int yi=y.getAsInt();