Android - JSON 解析不使用“setText”显示数据

我有以下 JSON 信息。


{

    "version": 2,

    "locations": [{

        "id": "750",

        "geo": {

            "name": "Lord Howe Island",

            "state": "Lord Howe Island",

            "country": {

                "id": "au",

                "name": "Australia"

            },

            "latitude": -31.557,

            "longitude": 159.086

        },

        "astronomy": {

            "objects": [{

                "name": "moon",

                "days": [{

                    "date": "2018-09-05",

                    "events": [],

                    "moonphase": "waningcrescent"

                }]

            }]

        }

    }]

}

我想要做的是将 JSON 中的一些细节(不是全部)打印到 textView,所以我想要的输出如下:


名称:豪勋爵岛国家:澳大利亚月相:残月


但是,在解析要在 textView 中打印的信息时,我没有任何运气。我目前正在使用以下代码:


JSONArray JA = new JSONArray(data);

        for (int i = 0; i < JA.length(); i++) {

            JSONObject JO = (JSONObject) JA.get(i);

            JSONObject geo = JO.getJSONObject("geo");

            JSONObject astronomy = JO.getJSONObject("astronomy");

            singleParsed = "Name:" + geo.get("name") + "\n" +

                    "Latitude:" + JO.get("latitude") + "\n" +

                    "Longitude:" + JO.get("longitude") + "\n" +

                    "Phase: " + astronomy.get("moonphase") + "\n";

        }

然后显示数据如下:


protected void onPostExecute(Void aVoid) {

        super.onPostExecute(aVoid);


        MainActivity.fetcheddata.setText(this.singleParsed);



    }

}

我可以只使用打印所有数据


MainActivity.fetcheddata.setText(this.data);

但是我也看到了 textView 和 ALL Information 中的标签,而不是我所追求的几个标签。


我究竟做错了什么?


非常感谢所有帮助

慕容3067478
浏览 178回答 3
3回答

FFIVE

您遇到的问题是您正在尝试使用 JSONObjects,即使位置是一个数组,对象也是如此。为此,您需要像这样迭代数组:&nbsp; &nbsp; JSONObject jo = new JSONObject(yourData);&nbsp; &nbsp; JSONArray locations = jo.getJSONArray("locations");&nbsp; &nbsp; for (int i = 0; i < locations.length(); ++i) {&nbsp; &nbsp; &nbsp; &nbsp; //iterate over each key, etc.&nbsp; &nbsp; }在我看来,在一天结束时,如果您更改密钥、添加密钥等,这将是一团糟,并且维护起来很痛苦。我建议您查看 GSON 之类的东西,然后只创建数据的视图模型你要。最后你会得到这样的东西:List<T> yourObjects = gson.fromJson(jsonReader, X[].class);然后您可以单独访问您想要的任何属性。

泛舟湖上清波郎朗

假设您的 json 数据原样以及您在上面写的输出:&nbsp; &nbsp; JSONArray locations = data.getJSONArray("location");&nbsp; &nbsp; JSONObject loc = locations.getJSONObject(0);&nbsp; &nbsp; JSONObject geo = loc.getJSONObject("geo");&nbsp; &nbsp; JSONObject country = geo.getJSONObject("country");&nbsp; &nbsp; JSONObject astronomy = loc.getJSONObject("astronomy");&nbsp; &nbsp; JSONObject objects = astronomy.getJSONArray("objects").getJSONObject(0);&nbsp; &nbsp; JSONObject day = objects.getJSONArray("days").getJSONObject(0);&nbsp; &nbsp; StringBuilder result = new StringBuilder();&nbsp; &nbsp; String singleParsed = "Name:" + geo.getString("name") + "\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Country: " + country.getString("name") +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Moon Phase: " + astronomy.getString("moonphase") + "\n";

繁花不似锦

不能moonphase直接从astronomy, []将定义一个数组,所以你可以这样做:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "astronomy": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "objects": [{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name": "moon",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "days": [{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "date": "2018-09-05",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "events": [],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "moonphase": "waningcrescent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }JSONObject astronomy = JO.getJSONObject("astronomy");JSONArray objects = astronomy.getJSONArray("objects");JSONArray days = objects.getJSONObject(0).getJSONArray("days");JSONObject moonphase = days.getJSONObject(0).get("moonphase");
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java