简体   繁体   中英

What kind of object in Java do i need to create for parsing an array in a JSON

我已经创建了用于解析此JSON的Java类和对象: openweathermap.org及其运行良好,但是有一部分我无法理解要为其构建的内容,其一部分是数组部分(第一行-名为“ weather”的对象),我一直试图弄清楚,但似乎没有任何效果,请帮忙吗?

You can use http://www.json.org/java/ It is very simple to use.

If you have a String JSON, you call just call in the constructor

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");

The simpleat class that all JSON Libraries can deserialize to is HashMap.class .

The simplest class for an array of objects is HashMap[].class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM