-4

我正在使用下面提到的 json 来解析数据及其崩溃

JSONObject eachData2 = user6.getJSONObject(n);

我正在尝试从数组中检索 lat/lng。请让我知道错误在哪里,以及我是否可以使 json 解析代码比下面的更短。我非常感谢任何帮助。在此先感谢。

        String jsonLocation = null;
        try {
            jsonLocation = converJsonToStringFromAssetFolder("datajson", getBaseContext());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        JSONObject mainObject = null;
        try {
            mainObject = new JSONObject(jsonLocation);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        JSONArray new1 = null;
        try {
            new1 = mainObject.getJSONArray("data1");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        for (int i = 0; i < new1.length(); i++) { 
            JSONObject c = null;
            try {
                c = (JSONObject) new1.get(i);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            JSONObject user = null;
            try {
                user = c.getJSONObject("latlng");
                JSONArray user2 = user.getJSONArray("geoset1");


                for (int j = 0; j < user2.length(); j++) {

                    JSONArray user3 = user2.getJSONArray(j);


                    for (int k = 0; k < user3.length(); k++) { 

                        JSONArray user4 = user3.getJSONArray(k);

                        for (int l = 0; l < user4.length(); l++) { 

                            JSONArray user5 = user4.getJSONArray(l);

                            for (int m = 0; m < user5.length(); m++) { 



                                JSONArray user6 = user5.getJSONArray(m);

                                 for (int n = 0; n < user6.length(); n++) { 




                                JSONObject eachData2 = user6.getJSONObject(n);

                                if (n==0) {
                                    JSONObject eachData = user6.getJSONObject(n);


                                } else {
                                    JSONObject secondData = user6.getJSONObject(n);
                                }
                            }
                        }

                    }

                }


            } }catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }





}

public static String converJsonToStringFromAssetFolder(String fileName,Context context) throws IOException {
    AssetManager manager = context.getAssets();
    InputStream file = manager.open(fileName);

    byte[] data = new byte[file.available()];
    file.read(data);
    file.close();
    return new String(data);
}

}

json

{
    "dataset": "dataset1",
    "data1": [
        {
            "dataset2": "dataset21",
            "dataset3": {
                "dataset31": 1,
                "LAT": 70.0187,
                "LON": "-141.0205"
            },
            "latlng": {
                "latlng1": "latlngset1",
                "geoset1": [
                    [
                        [
                            [
                                70.0187,
                                -141.0205
                            ],
                            [
                                70.1292,
                                -141.7291
                            ],
                            [
                                70.4515,
                                -144.8163
                            ],
                            [
                                70.7471,
                                -148.4583
                            ],
                            [
                                70.7923,
                                -151.1609
                            ]
                        ]
                    ],
                    [
                        [
                            [
                                71.147,
                                -152.6221
                            ],
                            [
                                71.4307,
                                -154.8853
                            ],
                            [
                                71.5232,
                                -156.7529
                            ],
                            [
                                71.5232,
                                -156.7529
                            ],
                            [
                                71.5232,
                                -156.7529
                            ]
                        ]
                    ],
                    [
                        [
                            [
                                71.1185,
                                -153.9954
                            ],
                            [
                                64.3922,
                                -167.0142
                            ],
                            [
                                64.0554,
                                -165.7343
                            ],
                            [
                                64.0193,
                                -163.2294
                            ],
                            [
                                63.9615,
                                -162.1143
                            ]
                        ]
                    ]
                ]
            }
        }
    ]
}
4

2 回答 2

1

你写了太多for循环:

user6int类型:int user6 = user5.getInt(m);

这是一个修复:

JSONObject mainObject = null;
    try {
        mainObject = new JSONObject(jsonString);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    JSONArray new1 = null;
    try {
        new1 = mainObject.getJSONArray("data1");
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    for (int i = 0; i < new1.length(); i++) { 
        JSONObject c = null;
        try {
            c = (JSONObject) new1.get(i);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        JSONObject user = null;
        try {
            user = c.getJSONObject("latlng");
            JSONArray user2 = user.getJSONArray("geoset1");


            for (int j = 0; j < user2.length(); j++) {

                JSONArray user3 = user2.getJSONArray(j);


                for (int k = 0; k < user3.length(); k++) { 

                    JSONArray user4 = user3.getJSONArray(k);

                    for (int l = 0; l < user4.length(); l++) { 

                        JSONArray user5 = user4.getJSONArray(l);

                        for (int m = 0; m < user5.length(); m++) { 



                            doubleuser6 = user5.getDouble(m);

                            System.out.println(user6);
                        }

                    }

                }


            } }catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

    }

输出:

70
-141
70
-141
70
-144
70
-148
70
-151
71
-152
71
-154
71
-156
71
-156
71
-156
71
-153
64
-167
64
-165
64
-163
63
-162 
于 2013-10-04T12:37:30.680 回答
0

您好,这看起来很复杂,但如果您愿意使用GSON,则只需几行即可将其转换为 JAVA 对象。

于 2013-10-04T12:30:58.927 回答