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



我一直在创建Java类和对象来解析这个JSON:openweathermap.org它的工作很棒,但有一部分我无法理解它的工作,它的数组部分(第一行-对象命名为"天气"),我一直试图找出它,但似乎没有工作,任何帮助请?

您可以使用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");

所有JSON库都可以反序列化到的简单类是HashMap.class

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

最新更新