繁体   English   中英

Json解析在Android中的问题

[英]Json Parsing in android issue

我正在为盲人开发一个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中获取人脸的类型值? 提前致谢。

应该是这样的...

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;
}

在上述回应中

Faces是具有一个值的JSONArray

获取Faces的JSONArray,然后从索引为0的Faces JSONArray中获取JSONObject,并获取所需的值。

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

如果您只想到达faces [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");
 ...
 ...
 }

...表示您将根据需要编写的代码。 祝您编程愉快。

有一个简单的解决方案可以轻松获得它。

  1. 使用此链接转换您的Json 会将您的json转换为POJO类

    结果您将获得一些课程。

  2. 通过在onPostExecute()方法中遵循演示代码,您可以获取Faces Data,

 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 (); 

这是您轻松轻松获得结果的简单方法。

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

假设您的回应是res

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM