Json解析android问题

时间:2016-04-08 11:10:21

标签: android json parsing

我正在为盲人开发一个Android应用程序,我使用了Karios api,一切都很顺利 我得到了api的输出

    {
"images": [
{
"time": 0.86679,
"status": "Complete",
"file": "face_57078c26c5baf.jpg",
"width": 776,
"height": 570,
"faces": [
{
"topLeftX": 94,
"topLeftY": 95,
"width": 189,
"height": 189,
"leftEyeCenterX": 152.275,
"leftEyeCenterY": 172.175,
"rightEyeCenterX": 225.5125,
"rightEyeCenterY": 168.2375,
"noseTipX": 188.83536147895,
"noseTipY": 209.93069059821,
"noseBtwEyesX": 188.69509888285,
"noseBtwEyesY": 163.76829692552,
"chinTipX": 194.90835566598,
"chinTipY": 272.94500076858,
"leftEyeCornerLeftX": 139.748828125,
"leftEyeCornerLeftY": 173.99609375,
"leftEyeCornerRightX": 167.2744140625,
"leftEyeCornerRightY": 173.6638671875,
"rightEyeCornerLeftX": 210.7591796875,
"rightEyeCornerLeftY": 171.3259765625,
"rightEyeCornerRightX": 236.955859375,
"rightEyeCornerRightY": 167.622265625,
"rightEarTragusX": -1,
"rightEarTragusY": -1,
"leftEarTragusX": -1,
"leftEarTragusY": -1,
"leftEyeBrowLeftX": 124.96433022747,
"leftEyeBrowLeftY": 160.66254675843,
"leftEyeBrowMiddleX": 144.98960402463,
"leftEyeBrowMiddleY": 151.01911416052,
"leftEyeBrowRightX": 171.91583787312,
"leftEyeBrowRightY": 155.84335467715,
"rightEyeBrowLeftX": 204.04144319738,
"rightEyeBrowLeftY": 153.33575045711,
"rightEyeBrowMiddleX": 228.74564647717,
"rightEyeBrowMiddleY": 144.48143172972,
"rightEyeBrowRightX": 247.64047899459,
"rightEyeBrowRightY": 149.93257330135,
"nostrilLeftHoleBottomX": 179.40484698779,
"nostrilLeftHoleBottomY": 222.21013668835,
"nostrilRightHoleBottomX": 200.05702183911,
"nostrilRightHoleBottomY": 220.59810540404,
"nostrilLeftSideX": 168.63097309489,
"nostrilLeftSideY": 217.27943710651,
"nostrilRightSideX": 211.08266584481,
"nostrilRightSideY": 213.96581724432,
"lipCornerLeftX": 164.95407560776,
"lipCornerLeftY": 244.11611360475,
"lipLineMiddleX": 192.40075144939,
"lipLineMiddleY": 240.81939551421,
"lipCornerRightX": 219.84742729102,
"lipCornerRightY": 237.52267742366,
"pitch": -1.1481443688526,
"yaw": 0.95481944449499,
"roll": -3.7557902314114,
"attributes": {
"gender": {
"time": 0.06871,
"type": "F",
"confidence": "90%"
}
}
}
]
}
]
}

如何在android中获取face的类型值?提前谢谢。

5 个答案:

答案 0 :(得分:0)

它应该是这样的......

private String returnPersonType(String output) {

    String type = "";

    try {
        JSONObject jsonObject = new JSONObject(output);
        if (jsonObject.has("images")) {
            JSONArray imagesArray = jsonObject.getJSONArray("images");
            if (imagesArray.length() > 0 && imagesArray.getJSONObject(0).has("faces")) {
                JSONArray facesArray = imagesArray.getJSONObject(0).getJSONArray("faces");
                if (facesArray.length() > 0 && facesArray.getJSONObject(0).has("attributes") && facesArray.getJSONObject(0).getJSONObject("attributes").has("gender")) {
                    type = facesArray.getJSONObject(0).getJSONObject("attributes").getJSONObject("gender").getString("type");
                }
            }
        }
    } catch (JSONException e) {

    }

    return type;
}

答案 1 :(得分:0)

以上回复

faces是一个带有一个值的JSONArray

获取面孔的JSONArray 然后从面向JSONArray的0索引获取JSONObject并获取所需的值。

答案 2 :(得分:0)

解析时要记住的第一件事 { - 需要调用JSONObject函数 [ - 需要调用JSONArray函数

如果您只想要到达面[0]对象,

可以解决您的问题
 {
 ....
 ....
 JSONObject jsonRootObject=new JSONObject("/*Your son data as string or any string variable containing your son data*/");
 JSONArray jsonArray=jsonRootObject.optJSONArray("images");
 JSONObject object0=jsonArray.optJSONObject(0);
 JSONArray faces=object0.optJSONArray("faces");
 JSONObject face0=faces.optJSONObject(0);
 //Now you can call any object data by using its key. following code gets height stored int it
 String t=face0.optString("height");
 ...
 ...
 }

...表示您将根据需要编写的代码。 祝你的节目顺利。

答案 3 :(得分:0)

有一个简单的解决方案可以轻松实现。

  1. 使用This link will convert your json to POJO class

    转换您的Json

    你会得到一些课程。

  2. 通过在onPostExecute()方法中跟踪演示代码,您可以获得Faces数据,
  3. String json = new Gson().toJson(object);
    		YourMainPOJO newProjectList = new Gson().fromJson(json,
    				YourMainPOJO.class);
    		ArrayList<YourImagePOJO> cpImageData = cp.getImages();
    		ArrayList<YourFacePOJO> cpFaceData = cpImageData. getFaces();
    		String topLeftY = cpFaceData.getTopLeftY ();
    		String topLeftX = cpFaceData.getTopLeftX ();

    这是一种简单的方法,您可以轻松获得结果,而不会有任何麻烦。

答案 4 :(得分:-1)

res.images[0].faces[0].attributes.gender.type

假设您的回复位于res