首页 > 解决方案 > Java - 从天气 api 获取特定的 json 信息

问题描述

我想从openweather API中提取当前天气状态作为字符串(如多云、下雨......) 。问题是,该信息位于天气:0:主要:(见这里) 我无法获取信息,因为有一个“0:”。我是 java 中 JSON 的新手,只需要你的帮助。

标签: javajsonapiopenweathermap

解决方案


我推荐你使用 Gson 转换器。它可以帮助您将 JSON 转换为 Java 类。

数据转换

  • JSON 对象 - Java 类
  • 数组 - 列表<>

有用的链接

您可以使用 jsonschema2pojo 将 JSON 转换为如下类:

-----------------------------------com.example.Clouds.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Clouds {

@SerializedName("all")
@Expose
private Integer all;

public Integer getAll() {
return all;
}

public void setAll(Integer all) {
this.all = all;
}

}
-----------------------------------com.example.Coord.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Coord {

@SerializedName("lon")
@Expose
private Integer lon;
@SerializedName("lat")
@Expose
private Integer lat;

public Integer getLon() {
return lon;
}

public void setLon(Integer lon) {
this.lon = lon;
}

public Integer getLat() {
return lat;
}

public void setLat(Integer lat) {
this.lat = lat;
}

}
-----------------------------------com.example.Data.java-----------------------------------

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Data {

@SerializedName("coord")
@Expose
private Coord coord;
@SerializedName("sys")
@Expose
private Sys sys;
@SerializedName("weather")
@Expose
private List<Weather> weather = null;
@SerializedName("main")
@Expose
private Main main;
@SerializedName("wind")
@Expose
private Wind wind;
@SerializedName("rain")
@Expose
private Rain rain;
@SerializedName("clouds")
@Expose
private Clouds clouds;
@SerializedName("dt")
@Expose
private Integer dt;
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("cod")
@Expose
private Integer cod;

public Coord getCoord() {
return coord;
}

public void setCoord(Coord coord) {
this.coord = coord;
}

public Sys getSys() {
return sys;
}

public void setSys(Sys sys) {
this.sys = sys;
}

public List<Weather> getWeather() {
return weather;
}

public void setWeather(List<Weather> weather) {
this.weather = weather;
}

public Main getMain() {
return main;
}

public void setMain(Main main) {
this.main = main;
}

public Wind getWind() {
return wind;
}

public void setWind(Wind wind) {
this.wind = wind;
}

public Rain getRain() {
return rain;
}

public void setRain(Rain rain) {
this.rain = rain;
}

public Clouds getClouds() {
return clouds;
}

public void setClouds(Clouds clouds) {
this.clouds = clouds;
}

public Integer getDt() {
return dt;
}

public void setDt(Integer dt) {
this.dt = dt;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getCod() {
return cod;
}

public void setCod(Integer cod) {
this.cod = cod;
}

}
-----------------------------------com.example.Main.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Main {

@SerializedName("temp")
@Expose
private Double temp;
@SerializedName("humidity")
@Expose
private Integer humidity;
@SerializedName("pressure")
@Expose
private Integer pressure;
@SerializedName("temp_min")
@Expose
private Double tempMin;
@SerializedName("temp_max")
@Expose
private Double tempMax;

public Double getTemp() {
return temp;
}

public void setTemp(Double temp) {
this.temp = temp;
}

public Integer getHumidity() {
return humidity;
}

public void setHumidity(Integer humidity) {
this.humidity = humidity;
}

public Integer getPressure() {
return pressure;
}

public void setPressure(Integer pressure) {
this.pressure = pressure;
}

public Double getTempMin() {
return tempMin;
}

public void setTempMin(Double tempMin) {
this.tempMin = tempMin;
}

public Double getTempMax() {
return tempMax;
}

public void setTempMax(Double tempMax) {
this.tempMax = tempMax;
}

}
-----------------------------------com.example.Rain.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Rain {

@SerializedName("3h")
@Expose
private Integer _3h;

public Integer get3h() {
return _3h;
}

public void set3h(Integer _3h) {
this._3h = _3h;
}

}
-----------------------------------com.example.Sys.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Sys {

@SerializedName("country")
@Expose
private String country;
@SerializedName("sunrise")
@Expose
private Integer sunrise;
@SerializedName("sunset")
@Expose
private Integer sunset;

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

public Integer getSunrise() {
return sunrise;
}

public void setSunrise(Integer sunrise) {
this.sunrise = sunrise;
}

public Integer getSunset() {
return sunset;
}

public void setSunset(Integer sunset) {
this.sunset = sunset;
}

}
-----------------------------------com.example.Weather.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Weather {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("main")
@Expose
private String main;
@SerializedName("description")
@Expose
private String description;
@SerializedName("icon")
@Expose
private String icon;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getMain() {
return main;
}

public void setMain(String main) {
this.main = main;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getIcon() {
return icon;
}

public void setIcon(String icon) {
this.icon = icon;
}

}
-----------------------------------com.example.Wind.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Wind {

@SerializedName("speed")
@Expose
private Double speed;
@SerializedName("deg")
@Expose
private Double deg;

public Double getSpeed() {
return speed;
}

public void setSpeed(Double speed) {
this.speed = speed;
}

public Double getDeg() {
return deg;
}

public void setDeg(Double deg) {
this.deg = deg;
}

}

完成后,您可以在 Java 中执行以下操作:

String config_settings = "Your JSON String";
Gson converter = new Gson();
ConfigSettings settings = converter.fromJson(config_settings , Data.class);

推荐阅读