我有以下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和所有信息中也看到了标签,而不是我想要的几个标签。
我做错了什么?
非常感谢您的帮助。
编辑:这是建议的,目前正在处理中。
JSONArray JA = new JSONArray(data);
for (int i = 0; i < JA.length(); i++) {
JSONObject JO = (JSONObject) JA.get(i);
JSONObject astronomy = JO.getJSONObject("astronomy");
JSONArray objects = astronomy.getJSONArray("objects");
JSONArray days = objects.getJSONObject(0).getJSONArray("days");
Object moonphase = days.getJSONObject(0).get("moonphase");
moon = "Name:" + days.get(Integer.parseInt("moonphase"));
}发布于 2018-09-12 09:32:20
您遇到的问题是您正在尝试使用JSONObjects,即使位置是一个数组,对象也是。
为了让它起作用,你需要像这样迭代数组:
JSONObject jo = new JSONObject(yourData);
JSONArray locations = jo.getJSONArray("locations");
for (int i = 0; i < locations.length(); ++i) {
//iterate over each key, etc.
}在我看来,在一天结束的时候,如果你改变键,添加键,等等,这将是一场混乱和痛苦的维护。我建议研究一下像GSON这样的东西,然后只创建你想要的数据的视图模型。最后,你会得到这样的结果:
List<T> yourObjects = gson.fromJson(jsonReader, X[].class);然后,您可以单独访问您想要的任何属性。
发布于 2018-09-12 09:14:45
无法直接从astronomy获取moonphase,[]将定义一个数组,因此您可以这样做:
"astronomy": {
"objects": [{
"name": "moon",
"days": [{
"date": "2018-09-05",
"events": [],
"moonphase": "waningcrescent"
}]
}]
}
JSONObject astronomy = JO.getJSONObject("astronomy");
JSONArray objects = astronomy.getJSONArray("objects");
JSONArray days = objects.getJSONObject(0).getJSONArray("days");
JSONObject moonphase = days.getJSONObject(0).get("moonphase");发布于 2018-09-12 09:36:00
假设您的json数据是这样的,并且您的输出是如上所写的:
JSONArray locations = data.getJSONArray("location");
JSONObject loc = locations.getJSONObject(0);
JSONObject geo = loc.getJSONObject("geo");
JSONObject country = geo.getJSONObject("country");
JSONObject astronomy = loc.getJSONObject("astronomy");
JSONObject objects = astronomy.getJSONArray("objects").getJSONObject(0);
JSONObject day = objects.getJSONArray("days").getJSONObject(0);
StringBuilder result = new StringBuilder();
String singleParsed = "Name:" + geo.getString("name") + "\n" +
"Country: " + country.getString("name") +
"Moon Phase: " + astronomy.getString("moonphase") + "\n";https://stackoverflow.com/questions/52286022
复制相似问题