[英]How do I fetch specific data from a JSON in java using getString() function
[英]How to fetch data from complex JSON using java
我是 android 应用程序开发的新手,我很震惊,因为我无法从 JSON 获取我需要的特定数据。
这是 JSON 文件。
{
"message": "",
"cod": "200",
"city_id": 4887398,
"calctime": 0.0863,
"cnt": 4,
"list": [
{
"main": {
"temp": 268.987,
"temp_min": 268.987,
"temp_max": 268.987,
"pressure": 1001.11,
"sea_level": 1024.68,
"grnd_level": 1001.11,
"humidity": 100
},
"wind": {
"speed": 5.06,
"deg": 291.002
},
"clouds": {
"all": 48
},
"weather": [
{
"id": 802,
"main": "Clouds",
"description": "scattered clouds",
"icon": "03d"
}
],
"dt": 1485703465
},
{
"main": {
"temp": 268.097,
"temp_min": 268.097,
"temp_max": 268.097,
"pressure": 1003.57,
"sea_level": 1027.08,
"grnd_level": 1003.57,
"humidity": 100
},
"wind": {
"speed": 8.56,
"deg": 314.007
},
"clouds": {
"all": 44
},
"weather": [
{
"id": 802,
"main": "Clouds",
"description": "scattered clouds",
"icon": "03d"
}
],
"dt": 1485730032
},
{
"main": {
"temp": 266.787,
"temp_min": 266.787,
"temp_max": 266.787,
"pressure": 1005.73,
"sea_level": 1029.63,
"grnd_level": 1005.73,
"humidity": 100
},
"wind": {
"speed": 6.79,
"deg": 316.012
},
"clouds": {
"all": 0
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "Sky is Clear",
"icon": "01n"
}
],
"dt": 1485755383
},
{
"main": {
"temp": 263.64,
"pressure": 1015,
"humidity": 57,
"temp_min": 262.15,
"temp_max": 265.15
},
"wind": {
"speed": 2.6,
"deg": 280
},
"clouds": {
"all": 1
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "sky is clear",
"icon": "01n"
}
],
"dt": 1485780512
}
]
}
我要获取的是主要和天气 object 的所有值的描述,例如我试图在我的屏幕上打印它。如何循环以获得这个结果?
"main": "Clouds",
"description": "scattered clouds",
"main": "Clouds",
"description": "scattered clouds",
"main": "Clear",
"description": "Sky is Clear",
"main": "Clear",
"description": "Sky is Clear",
谢谢你
您可以使用 Jackson 来实现它。
对于您的应用,您需要这些库:
Jackson 数据绑定: https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-datad/2.1
Jackson 内核: https://mvnrepository.com/artifact/com.fasterxml.ZB41779690B83F182ACC67D6388C7BAC911Z.core/jackson-core.2/
public static void main(String[] args) throws JsonProcessingException {
String json = "your json as string";
JsonNode root = (new ObjectMapper()).readTree(json); //JsonProcessingException
//check if root has node list
if(root.has("list")){
//get node list from root
JsonNode list = root.get("list");
//check if list is an array node
if(list.isArray()){
//iterate list
for(JsonNode listElem : list){
//check if current node fromt list has node weather
if(listElem.has("weather")){
//get node weather
JsonNode weather = listElem.get("weather");
//check if weather is array
if(weather.isArray()){
for(JsonNode weatherElem : weather){
if(weatherElem.has("main") && weatherElem.has("description")){
System.out.println(weatherElem.get("main") + " - " + weatherElem.get("description"));
}
}
}
}
}
}
}
}
但是,如果您确定 json 结构始终相同。 您可以跳过所有检查。
public static void main(String[] args) throws JsonProcessingException {
String json = "your json as string";
JsonNode root = (new ObjectMapper()).readTree(json); //JsonProcessingException
for(JsonNode listElem : root.get("list")){
for(JsonNode weatherElem : listElem.get("weather")) {
System.out.println(weatherElem.get("main") + " - " + weatherElem.get("description"));
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.