从嵌套的json字符串中提取double并打印它

时间:2021-08-01 16:02:47

I'm trying to parse numbers from a json string and print them onto a file.

But for this I want to access elements inside an array which happens to have another object like structure which is quite confusing for a beginner like me.

I'm trying this in java so any help would be appreciated.

This is the json string

我正在尝试从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"

}

This is my java code and it works fine till line 18, but unfortunately it gives me an exception at line 19

这是我的java代码,直到第18行才能正常工作,但不幸的是它在第19行给了我一个例外


    package faceplus;

    import org.json.JSONArray;
    import org.json.JSONObject;
    import java.io.File;
    import org.apache.commons.io.FileUtils;

    public class FacePlus {

        @SuppressWarnings("unchecked")
        public static void main(String[] args) {
            try {
                File file = new File("C:\\Users\\sandesh\\Documents\\NetBeansProjects\\FacePlusPlus\\src\\faceplusplus\\file1.json");
                String s1 = FileUtils.readFileToString(file);
                JSONObject root = new JSONObject(s1);
                JSONArray sportsArray = root.getJSONArray("result");
                JSONObject landmark = sportsArray.getJSONObject(0);
                JSONObject mouth_upper_lip_left_contour2 = landmark.getJSONObject("mouth_upper_lip_left_contour2");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


This is the exception

这是个例外

`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)`


Ive printed the landmark object which seems to be fine plus I also tried to output the attribute "face_id"

香港专业教育学院印刷了看似很好的标志性物品加上我也尝试输出属性“face_id”

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


this gives output:
{"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.439375,"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_quarter":{"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.211458,"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

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

1 个解决方案

#1


3  

you can try this code block:

你可以尝试这个代码块:

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

you have missed to get JSONObject for landmark

你错过了JSONObject的标志性建筑

#1


3  

you can try this code block:

你可以尝试这个代码块:

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

you have missed to get JSONObject for landmark

你错过了JSONObject的标志性建筑