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

时间:2016-02-02 05:29:13

标签: java json

我试图从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行给了我一个例外


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


这是个例外

`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""界标" {" 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& #34; {" X":30.875208," Y":38.792167}" right_eye_left_corner" {" X":62.163958,& #34; Y":42.366333}" left_eye_pupil" {" X":32.3075" Y":40.962}" right_eyebrow_lower_middle&# 34;:{" X":73.863333," Y":31.333167}" left_eye_upper_right_quarter" {" X":35.361667,&# 34; Y":39.773333}" mouth_lower_lip_left_contour1" {" X":40.790625," Y":75.426833}" right_eye_top&#34 ;:{" X":71.181458," Y":38.7445}" mouth_lower_lip_left_contour3" {" X":41.727292,&#34 ; Y":80.028667}" mouth_lower_lip_left_contour2" {" X":36.259375," Y":75.988833}" right_eye_bottom" :{" X&#34 ;: 71.683333," Y":42.5745}" nose_contour_right3" {" X":57.44375" Y":62.9135}&#34 ; 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& #34; {" X":63.582708," Y":75.625667}" nose_contour_left3" {" X":43.509375,& #34; Y":62.931167}" mouth_lower_lip_right_contour2" {" X":67.871875," Y":75.885167}" nose_contour_left2&# 34;:{" X":41.098125," Y":54.835333}" mouth_lower_lip_right_contour3" {" X":60.077292,&# 34; Y":80.073667}" mouth_upper_lip_left_contour 3" {" 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& #34; {" X":27.828542," Y":31.549}" right_eye_lower_left_quarter" {" X":66.72625,& #34; Y":42.326333}" left_eyebrow_upper_left_quarter" {" X":21.247083," Y":30.283333}" mouth_upper_lip_left_contour1&# 34;:{" X":47.247917," Y":69.0625}" mouth_upper_lip_left_contour2" {" X":38.365625,&# 34; Y":69.744333}" contour_chin" {" X":51.339375," Y":96.31}" right_eye_center&#34 ;:{" X":71.312917," Y":40.730667}" left_eye_center" {" X":30.930833,&#34 ; Y":41.167667}" right_eyebrow_upper_right_quarter" {" X":79.733125," Y":29.779167}" contour_right7" :{" X":78.894583," Y":85.205167},&#3 4; left_eye_lower_left_quarter" {" X":26.42125" Y":42.6945}" mouth_right_corner" {" X&#34 ;: 72.33625" Y":70.449333}" contour_right8" {" X":70.933958," Y":90.280167}&#34 ; 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& #34; {" X":51.08625" Y":77.392833}" right_eye_pupil" {" X":71.348542,& #34; Y":40.599833}" mouth_upper_lip_right_contour2" {" X":63.850833," Y":69.407167}" left_eyebrow_upper_middle&# 34;:{" X":28.171667," Y":29.42 5833}," 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& #34;: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&# 34;: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&#34 ;:16.962646," Y":33.749}" contour_left4" {" X":10.959458," Y":64.880833},& #34; contour_left5&#3 4;:{" X":13.481042," Y":72.296667}" left_eye_upper_left_quarter" {" X":26.438542,&# 34; Y":39.500833}" contour_left6" {" X":18.146583," Y":78.593667}" contour_left7&#34 ;:{" X":24.153542," Y":85.123167}" contour_left8" {" X":31.56625&#34 ; 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," ý":43.398167}" left_eyebrow_right_corner" {" X":40.150417," Y":33.2645}" right_eye_upper_left_quarter&#34 ;: {" X":66.551458," Y":39.570333}" contour_right1" {" X":92.211458," Y& #34;:41.189833}" contour_right2" {" X":92.823542," Y":48.95}" nose_right":{ " X":64.148125," Y":60.715333}" contour_right3" {" X":92.1," Y&# 34;: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& #34; {" X":88.356458," Y":72.925333}" contour_right6" {" X":84.246042,& #34; Y":79.771833}" contour_left3" {" X":9.674896," Y":57.225667}" left_eyebrow_upper_right_quarter&# 34;:{" X":34.1775" Y":29.944333}}} 4d78dafb9aec7f8f9e2c0c79bebebfc1

1 个答案:

答案 0 :(得分:3)

你可以尝试这个代码块:

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的标志性