【问题标题】:Extract double from a nested json string and print it从嵌套的 json 字符串中提取 double 并打印
【发布时间】:2016-02-02 05:29:12
【问题描述】:

我正在尝试从 json 字符串中解析数字并将它们打印到文件中。

但为此,我想访问一个数组中的元素,该数组恰好有另一个类似结构的对象,这对于像我这样的初学者来说相当混乱。

我正在 java 中尝试这个,所以任何帮助将不胜感激。

这是json字符串

{
"result": [{
        "landmark": {
            "mouth_upper_lip_left_contour2": {
                "y": 69.744333,
                "x": 38.365625
            },
            "contour_chin": {
                "y": 96.31,
                "x": 51.339375
            },
            "mouth_lower_lip_right_contour3": {
                "y": 80.073667,
                "x": 60.077292
            },
            "contour_right9": {
                "y": 94.413333,
                "x": 63.012917
            },
            "mouth_upper_lip_left_contour1": {
                "y": 69.0625,
                "x": 47.247917
            },
            "right_eyebrow_lower_middle": {
                "y": 31.333167,
                "x": 73.863333
            },
            "left_eyebrow_lower_middle": {
                "y": 31.549,
                "x": 27.828542
            },
            "mouth_upper_lip_left_contour3": {
                "y": 71.7415,
                "x": 41.557292
            },
            "left_eyebrow_lower_left_quarter": {
                "y": 31.958833,
                "x": 21.760625
            },
            "right_eyebrow_lower_left_quarter": {
                "y": 32.3125,
                "x": 67.909583
            },
            "right_eyebrow_lower_right_quarter": {
                "y": 31.311333,
                "x": 80.055208
            },
            "nose_contour_left1": {
                "y": 42.271667,
                "x": 45.802083
            },
            "left_eyebrow_upper_left_quarter": {
                "y": 30.283333,
                "x": 21.247083
            },
            "left_eye_bottom": {
                "y": 43.398167,
                "x": 31.089375
            },
            "mouth_lower_lip_bottom": {
                "y": 81.3855,
                "x": 50.812708
            },
            "contour_right7": {
                "y": 85.205167,
                "x": 78.894583
            },
            "left_eyebrow_left_corner": {
                "y": 33.749,
                "x": 16.962646
            },
            "contour_right6": {
                "y": 79.771833,
                "x": 84.246042
            },
            "right_eye_lower_right_quarter": {
                "y": 41.866667,
                "x": 75.888542
            },
            "right_eye_bottom": {
                "y": 42.5745,
                "x": 71.683333
            },
            "contour_left7": {
                "y": 85.123167,
                "x": 24.153542
            },
            "contour_left6": {
                "y": 78.593667,
                "x": 18.146583
            },
            "contour_left5": {
                "y": 72.296667,
                "x": 13.481042
            },
            "contour_left4": {
                "y": 64.880833,
                "x": 10.959458
            },
            "contour_left3": {
                "y": 57.225667,
                "x": 9.674896
            },
            "contour_left2": {
                "y": 50.2025,
                "x": 8.638583
            },
            "contour_left1": {
                "y": 41.911,
                "x": 9.503813
            },
            "left_eye_lower_left_quarter": {
                "y": 42.6945,
                "x": 26.42125
            },
            "contour_right1": {
                "y": 41.189833,
                "x": 92.211458
            },
            "contour_right3": {
                "y": 57.5375,
                "x": 92.1
            },
            "contour_right2": {
                "y": 48.95,
                "x": 92.823542
            },
            "contour_right5": {
                "y": 72.925333,
                "x": 88.356458
            },
            "contour_right4": {
                "y": 65.2235,
                "x": 90.749583
            },
            "contour_left9": {
                "y": 94.546667,
                "x": 40.728333
            },
            "contour_left8": {
                "y": 90.514667,
                "x": 31.56625
            },
            "nose_right": {
                "y": 60.715333,
                "x": 64.148125
            },
            "right_eye_upper_right_quarter": {
                "y": 39.302,
                "x": 75.846042
            },
            "nose_contour_right3": {
                "y": 62.9135,
                "x": 57.44375
            },
            "nose_contour_lower_middle": {
                "y": 64.059167,
                "x": 50.642083
            },
            "right_eye_top": {
                "y": 38.7445,
                "x": 71.181458
            },
            "right_eye_right_corner": {
                "y": 41.363,
                "x": 78.832917
            },
            "mouth_upper_lip_right_contour1": {
                "y": 69.000667,
                "x": 55.375625
            },
            "mouth_upper_lip_right_contour2": {
                "y": 69.407167,
                "x": 63.850833
            },
            "mouth_upper_lip_right_contour3": {
                "y": 71.284167,
                "x": 62.032292
            },
            "left_eye_left_corner": {
                "y": 41.623,
                "x": 22.466042
            },
            "mouth_right_corner": {
                "y": 70.449333,
                "x": 72.33625
            },
            "mouth_lower_lip_right_contour1": {
                "y": 75.625667,
                "x": 63.582708
            },
            "contour_right8": {
                "y": 90.280167,
                "x": 70.933958
            },
            "left_eyebrow_right_corner": {
                "y": 33.2645,
                "x": 40.150417
            },
            "right_eye_center": {
                "y": 40.730667,
                "x": 71.312917
            },
            "left_eye_upper_right_quarter": {
                "y": 39.773333,
                "x": 35.361667
            },
            "mouth_upper_lip_top": {
                "y": 69.256833,
                "x": 51.340625
            },
            "nose_left": {
                "y": 60.0965,
                "x": 37.100625
            },
            "left_eye_upper_left_quarter": {
                "y": 39.500833,
                "x": 26.438542
            },
            "left_eye_center": {
                "y": 41.167667,
                "x": 30.930833
            },
            "left_eye_lower_right_quarter": {
                "y": 42.8465,
                "x": 35.399167
            },
            "right_eyebrow_left_corner": {
                "y": 33.184833,
                "x": 61.318958
            },
            "right_eye_left_corner": {
                "y": 42.366333,
                "x": 62.163958
            },
            "mouth_lower_lip_top": {
                "y": 77.392833,
                "x": 51.08625
            },
            "right_eyebrow_right_corner": {
                "y": 32.929667,
                "x": 84.607917
            },
            "mouth_lower_lip_left_contour1": {
                "y": 75.426833,
                "x": 40.790625
            },
            "left_eye_pupil": {
                "y": 40.962,
                "x": 32.3075
            },
            "mouth_left_corner": {
                "y": 70.978333,
                "x": 31.551458
            },
            "right_eyebrow_upper_left_quarter": {
                "y": 29.9845,
                "x": 67.1625
            },
            "right_eyebrow_upper_right_quarter": {
                "y": 29.779167,
                "x": 79.733125
            },
            "right_eye_lower_left_quarter": {
                "y": 42.326333,
                "x": 66.72625
            },
            "nose_tip": {
                "y": 58.352833,
                "x": 50.439375
            },
            "right_eye_upper_left_quarter": {
                "y": 39.570333,
                "x": 66.551458
            },
            "left_eyebrow_upper_middle": {
                "y": 29.425833,
                "x": 28.171667
            },
            "mouth_lower_lip_right_contour2": {
                "y": 75.885167,
                "x": 67.871875
            },
            "mouth_lower_lip_left_contour3": {
                "y": 80.028667,
                "x": 41.727292
            },
            "nose_contour_left2": {
                "y": 54.835333,
                "x": 41.098125
            },
            "nose_contour_left3": {
                "y": 62.931167,
                "x": 43.509375
            },
            "nose_contour_right1": {
                "y": 42.119,
                "x": 56.340625
            },
            "nose_contour_right2": {
                "y": 54.3045,
                "x": 60.037917
            },
            "mouth_lower_lip_left_contour2": {
                "y": 75.988833,
                "x": 36.259375
            },
            "right_eyebrow_upper_middle": {
                "y": 29.119333,
                "x": 73.216875
            },
            "left_eyebrow_lower_right_quarter": {
                "y": 32.342667,
                "x": 33.74875
            },
            "left_eye_top": {
                "y": 38.792167,
                "x": 30.875208
            },
            "left_eye_right_corner": {
                "y": 42.65,
                "x": 39.908958
            },
            "left_eyebrow_upper_right_quarter": {
                "y": 29.944333,
                "x": 34.1775
            },
            "right_eye_pupil": {
                "y": 40.599833,
                "x": 71.348542
            },
            "mouth_upper_lip_bottom": {
                "y": 72.635667,
                "x": 51.448125
            }
        },
        "face_id": "4d78dafb9aec7f8f9e2c0c79bebebfc1"
    }],
"session_id": "8d8947da289b4beda33882f4fc8fe680"

}

这是我的 java 代码,它在第 18 行之前运行良好,但不幸的是它在第 19 行给了我一个异常

包faceplus; 导入 org.json.JSONArray; 导入 org.json.JSONObject; 导入java.io.File; 导入 org.apache.commons.io.FileUtils; 公共类 FacePlus { @SuppressWarnings("未选中") 公共静态无效主要(字符串[]参数){ 尝试 { 文件 file = new File("C:\\Users\\sandesh\\Documents\\NetBeansProjects\\FacePlusPlus\\src\\faceplusplus\\file1.json"); 字符串 s1 = FileUtils.readFileToString(file); JSONObject 根 = 新 JSONObject(s1); JSONArray sportsArray = root.getJSONArray("结果"); JSONObject 地标 = sportsArray.getJSONObject(0); JSONObjectmouth_upper_lip_left_contour2 = landmark.getJSONObject("mouth_upper_lip_left_contour2"); } 捕捉(异常 e){ e.printStackTrace(); } } }


这是例外

`run:
org.json.JSONException: JSONObject["mouth_upper_lip_left_contour2"] not found.
    at org.json.JSONObject.get(JSONObject.java:475)
    at org.json.JSONObject.getJSONObject(JSONObject.java:640)
    at faceplus.FacePlus.main(FacePlus.java:18)
BUILD SUCCESSFUL (total time: 0 seconds)`


我打印了似乎很好的地标对象,而且我还尝试输出属性“face_id”

String id = landmark.getString("face_id");
        System.out.println(landmark.toString());
        System.out.println(id);


这给出了输出:
{"face_id":"4d78dafb9aec7f8f9e2c0c79bebebfc1","landmark":{"left_eyebrow_lower_left_quarter":{"x":21.760625,"y":31.958833},"left_eye_lower_right_quarter":{"x":35.399167,"y":42.8465}, "left_eye_top":{"x":30.875208,"y":38.792167},"right_eye_left_corner":{"x":62.163958,"y":42.366333},"left_eye_pupil":{"x":32.3075,"y" :40.962},"right_eyebrow_lower_middle":{"x":73.863333,"y":31.333167},"left_eye_upper_right_quarter":{"x":35.361667,"y":39.773333},"mouth_lower_lip_left_contour1":{"x":40.790625 ,"y":75.426833},"right_eye_top":{"x":71.181458,"y":38.7445},"mouth_lower_lip_left_contour3":{"x":41.727292,"y":80.028667},"mouth_lower_lip_left_contour2":{" x":36.259375,"y":75.988833},"right_eye_bottom":{"x":71.683333,"y":42.5745},"nose_contour_right3":{"x":57.44375,"y":62.9135},"right_eye_upper_right_quarter ":{"x":75.846042,"y":39.302},"nose_contour_right1":{"x":56.340625,"y":42.119},"nose_contour_right2":{"x":60.037917,"y":54.3045 },"left_eye_right_corner":{"x":39.908958,"y":42.65},"nose_tip":{"x":50.43937 5,"y":58.352833},"right_eyebrow_upper_middle":{"x":73.216875,"y":29.119333},"mouth_lower_lip_right_contour1":{"x":63.582708,"y":75.625667},"nose_contour_left3":{ "x":43.509375,"y":62.931167},"mouth_lower_lip_right_contour2":{"x":67.871875,"y":75.885167},"nose_contour_left2":{"x":41.098125,"y":54.835333}," mouth_lower_lip_right_contour3":{"x":60.077292,"y":80.073667},"mouth_upper_lip_left_contour3":{"x":41.557292,"y":71.7415},"nose_contour_left1":{"x":45.802083,"y": 42.271667},"mouth_upper_lip_bottom":{"x":51.448125,"y":72.635667},"left_eyebrow_lower_middle":{"x":27.828542,"y":31.549},"right_eye_lower_left_quarter":{"x":66.72625, "y":42.326333},"left_eyebrow_upper_left_quarter":{"x":21.247083,"y":30.283333},"mouth_upper_lip_left_contour1":{"x":47.247917,"y":69.0625},"mouth_upper_lip_left_contour2":{"x ":38.365625,"y":69.744333},"contour_chin":{"x":51.339375,"y":96.31},"right_eye_center":{"x":71.312917,"y":40.730667},"left_eye_center" :{"x":30.930833,"y":41.167667},"right_eyebrow_upper_right_q uarter":{"x":79.733125,"y":29.779167},"contour_right7":{"x":78.894583,"y":85.205167},"left_eye_lower_left_quarter":{"x":26.42125,"y": 42.6945},"mouth_right_corner":{"x":72.33625,"y":70.449333},"contour_right8":{"x":70.933958,"y":90.280167},"contour_right9":{"x":63.012917, "y":94.413333},"right_eyebrow_upper_left_quarter":{"x":67.1625,"y":29.9845},"right_eye_right_corner":{"x":78.832917,"y":41.363},"left_eyebrow_lower_right_quarter":{"x ":33.74875,"y":32.342667},"right_eyebrow_right_corner":{"x":84.607917,"y":32.929667},"nose_left":{"x":37.100625,"y":60.0965},"mouth_lower_lip_top" :{"x":51.08625,"y":77.392833},"right_eye_pupil":{"x":71.348542,"y":40.599833},"mouth_upper_lip_right_contour2":{"x":63.850833,"y":69.407167} ,"left_eyebrow_upper_middle":{"x":28.171667,"y":29.425833},"mouth_upper_lip_right_contour3":{"x":62.032292,"y":71.284167},"mouth_upper_lip_right_contour1":{"x":55.375625,"y ":69.000667},"mouth_upper_lip_top":{"x":51.340625,"y":69.256833},"right_eye_lower_right_quarter":{"x":75. 888542,"y":41.866667},"mouth_left_corner":{"x":31.551458,"y":70.978333},"left_eye_left_corner":{"x":22.466042,"y":41.623},"nose_contour_lower_middle":{ "x":50.642083,"y":64.059167},"right_eyebrow_left_corner":{"x":61.318958,"y":33.184833},"right_eyebrow_lower_left_quarter":{"x":67.909583,"y":32.3125}," right_eyebrow_lower_right_quarter":{"x":80.055208,"y":31.311333},"left_eyebrow_left_corner":{"x":16.962646,"y":33.749},"contour_left4":{"x":10.959458,"y": 64.880833},"contour_left5":{"x":13.481042,"y":72.296667},"left_eye_upper_left_quarter":{"x":26.438542,"y":39.500833},"contour_left6":{"x":18.146583, "y":78.593667},"contour_left7":{"x":24.153542,"y":85.123167},"contour_left8":{"x":31.56625,"y":90.514667},"contour_left9":{"x ":40.728333,"y":94.546667},"mouth_lower_lip_bottom":{"x":50.812708,"y":81.3855},"left_eye_bottom":{"x":31.089375,"y":43.398167},"left_eyebrow_right_corner" :{"x":40.150417,"y":33.2645},"right_eye_upper_left_quarter":{"x":66.551458,"y":39.570333},"contour_right1":{"x":92.2 11458,"y":41.189833},"contour_right2":{"x":92.823542,"y":48.95},"nose_right":{"x":64.148125,"y":60.715333},"contour_right3":{ "x":92.1,"y":57.5375},"contour_left1":{"x":9.503813,"y":41.911},"contour_right4":{"x":90.749583,"y":65.2235}," contour_left2":{"x":8.638583,"y":50.2025},"contour_right5":{"x":88.356458,"y":72.925333},"contour_right6":{"x":84.246042,"y": 79.771833},"contour_left3":{"x":9.674896,"y":57.225667},"left_eyebrow_upper_right_quarter":{"x":34.1775,"y":29.944333}}} 4d78dafb9aec7f8f9e2c0c79bebebfc1

【问题讨论】:

  • landmark.getJSONObject(0) 告诉我它不能有 int 作为参数

标签: java json


【解决方案1】:

你可以试试这个代码块:

try {
        File file = new File("D:\\prectice\\JSONPrectice\\file.json");
        String s1 = FileUtils.readFileToString(file);
        JSONObject root = new JSONObject(s1);
        JSONArray sportsArray = root.getJSONArray("result");
        JSONObject arrayObject = sportsArray.getJSONObject(0);
        JSONObject landmark = arrayObject.getJSONObject("landmark");

        JSONObject mouth_upper_lip_left_contour2 = landmark.getJSONObject("mouth_upper_lip_left_contour2");

    } catch (Exception e) {
        e.printStackTrace();
    }

您错过了获取地标的 JSONObject

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-22
    • 1970-01-01
    • 1970-01-01
    • 2018-11-27
    • 2018-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多