Category Hierarchy

我遇到了一些麻烦,因为我在学习如何使用JSON simple解析json时遇到了障碍。

使自己简明扼要;

我正在尝试从一个url解析这段JSON。

"hourly": {

"summary": "Clear throughout the day.",

"icon": "clear-day",

"data": [

{

"time": 1550379600,

"summary": "Clear",

"icon": "clear-day",

"precipIntensity": 0,

"precipProbability": 0,

"temperature": 20.18,

"apparentTemperature": 14.31,

"dewPoint": 13.79,

"humidity": 0.76,

"pressure": 1024.47,

"windSpeed": 4.08,

"windGust": 5.25,

"windBearing": 30,

"cloudCover": 0.07,

"uvIndex": 0,

"visibility": 10,

"ozone": 342.67

}

因此,在使用json simple时,我就是这样解析这个JSON的

try{

String genreJson = IOUtils.toString(new URL(url));

JSONObject genreJsonObject = (JSONObject) JSONValue.parseWithException(genreJson);

//get the title

System.out.println(genreJsonObject.get("hourly")); //THIS WORKS

//System.out.println(genreJsonObject.get("visibility"));

//get the data

JSONArray genreArray = (JSONArray) genreJsonObject.get(0);

//get the first genre

//JSONObject firstGenre = (JSONObject) genreArray.get(0);

//System.out.println(firstGenre.get("data"));

}

catch (IOException | ParseException e){

e.printStackTrace();

}

因此,在调用System.out.println(genreJsonObject.get("hourly"));时,我得到了标题为"hourly“的方括号中的所有内容。我的意图是解析“每小时”括号中的数据,尽管我不知道如何解析标题括号中的标题。具体地说,我需要时间、precipProbability、precipIntensity和precipProbability (本例中没有这个属性)。

作为我第一次参加黑客马拉松的一部分,我对任何细节的缺失表示歉意,我现在正努力不想睡着。

我非常感谢任何人的帮助。

转载请注明出处:http://www.shandongyidao.com/article/20230526/1850366.html