簡體   English   中英

我需要在Java中創建哪種對象才能解析JSON中的數組

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

我已經創建了用於解析此JSON的Java類和對象: openweathermap.org及其運行良好,但是有一部分我無法理解要為其構建的內容,其一部分是數組部分(第一行-名為“ weather”的對象),我一直試圖弄清楚,但似乎沒有任何效果,請幫忙嗎?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM