我需要创建什么样的Java对象来解析JSON中的数组

时间:2015-08-21 22:54:43

标签: java json

我一直在创建用于解析此JSON的Java类和对象: openweathermap.org 并且它工作得很好,但有一部分我无法理解要为它工作的内容,它的数组部分(第一行 - 对象名为"天气"),我一直想弄清楚但似乎没有什么工作,请帮忙吗?

2 个答案:

答案 0 :(得分:0)

您可以使用http://www.json.org/java/使用起来非常简单。

如果你有一个String JSON,你只需要在构造函数中调用

String jsonStr = "{\"coord\":{\"lon\":-0.13,\"lat\":51.51},\"weather\":[{\"id\":721,\"main\":\"Haze\",\"description\":\"haze\",\"icon\":\"50n\"}],\"base\":\"cmc stations\",\"main\":{\"temp\":290.69,\"pressure\":1019,\"humidity\":72,\"temp_min\":287.04,\"temp_max\":294.15},\"wind\":{\"speed\":2.1,\"deg\":200},\"clouds\":{\"all\":0},\"dt\":1440199100,\"sys\":{\"type\":1,\"id\":5091,\"message\":0.005,\"country\":\"GB\",\"sunrise\":1440132986,\"sunset\":1440184167},\"id\":2643743,\"name\":\"London\",\"cod\":200}";

JSONObject json = new JSONObject(jsonStr);
JSONArray weather = json.getJSONArray("weather");

答案 1 :(得分:0)

所有JSON库可以反序列化的simpleat类是HashMap.class

对象数组的最简单类是HashMap[].class