gpt4 book ai didi

java - 解析JSON字符串的最简单方法

转载 作者:太空宇宙 更新时间:2023-11-03 13:36:53 25 4
gpt4 key购买 nike

如何将 JSON 字符串从一个键导航到另一个嵌套键并获取值?我有以下字符串

{ "data" : { "current_condition" : [ { "cloudcover" : "75",
"humidity" : "29",
"observation_time" : "07:59 PM",
"precipMM" : "0.0",
"pressure" : "1011",
"temp_C" : "19",
"temp_F" : "67",
"visibility" : "16",
"weatherCode" : "116",
"weatherDesc" : [ { "value" : "Partly Cloudy" } ],
"weatherIconUrl" : [ { "value" : "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png" } ],
"winddir16Point" : "N",
"winddirDegree" : "350",
"windspeedKmph" : "26",
"windspeedMiles" : "16"
} ],
"request" : [ { "query" : "01801",
"type" : "Zipcode"
} ],
"weather" : [ { "date" : "2011-05-09",
"precipMM" : "0.0",
"tempMaxC" : "19",
"tempMaxF" : "65",
"tempMinC" : "10",
"tempMinF" : "50",
"weatherCode" : "113",
"weatherDesc" : [ { "value" : "Sunny" } ],
"weatherIconUrl" : [ { "value" : "http://www/images/wsymbols01_png_64/wsymbol_0001_sunny.png" } ],
"winddir16Point" : "NNW",
"winddirDegree" : "348",
"winddirection" : "NNW",
"windspeedKmph" : "24",
"windspeedMiles" : "15"
},
{ "date" : "2011-05-10",
"precipMM" : "0.1",
"tempMaxC" : "13",
"tempMaxF" : "56",
"tempMinC" : "12",
"tempMinF" : "53",
"weatherCode" : "122",
"weatherDesc" : [ { "value" : "Overcast" } ],
"weatherIconUrl" : [ { "value" : "http://www/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png" } ],
"winddir16Point" : "NNE",
"winddirDegree" : "12",
"winddirection" : "NNE",
"windspeedKmph" : "31",
"windspeedMiles" : "19"
}
]
} }

所以我回答我自己的问题:如果其他人想快速获得值(value):这就是我要找的东西。

JSONObject j = new JSONObject(strResponse);

String weatherDesc = jObject.getJSONObject("data").getJSONArray("weather").getJSONObject(0).getJSONAr­ray("weatherDesc").getJSONObject(0).getString("value");

最佳答案

几乎所有语言都有 JSON 库。如果您提出一个建议,我也许可以为您指出一些事情。

同时,这里有一些:

等等。我建议快速谷歌搜索您选择的语言。

关于java - 解析JSON字符串的最简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5942737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com