使用GSON将JSON字符串转换为Java对象

时间:2018-08-07 10:39:04

标签: java json gson

我正在尝试将JSON字符串解析为Java对象。 JSON字符串如下:

{
"token":"Hn2jqNYe75dOY5Xj2BmZTLAB",
"team_id":"T394M2RS5",
"api_app_id":"AC1UE8Y4C",
"event":{
  "type":"message",
  "user":"UC1C1D059",
  "text":"test",
  "client_msg_id":"bf824b77-c2ff-4cf3-b770-278168d006fb",
  "ts":"1533637676.000135",
  "channel":"DC2A6V4SZ",
  "event_ts":"1533637676.000135",
  "channel_type":"app_home"
},
"type":"event_callback",
"authed_teams":[
  "T394M2RS5"
],
"event_id":"EvC5BPR1N2",
"event_time":1533637676
}

我正在使用GSON进行转换,但是我不知道如何 设计Java类,因为JSON包含另一个对象(事件对象)。 任何建议如何进行转换? 预先感谢。

3 个答案:

答案 0 :(得分:0)

尝试遵循文档。它可以帮助您将JSON转换为Java类。

数据转换

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

有用的链接

您可以使用jsonschema2pojo将JSON转换为 this 这样的类:

-----------------------------------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);

答案 1 :(得分:0)

这很简单:

  1. 将每个JSON键映射到POJO属性
  2. 使用@SerializedName自定义JSON密钥
  3. 将嵌套的JSON对象映射到类
  4. []映射到数组
  5. {}映射到对象

这是您的结果:     公共类YourClass {

    private String token;

    @SerializedName("team_id"
    private String teamId;

    @SerializedName("api_app_id")
    private String apiAppId;

    private Event event;

    private String type;

    @SerializedName("authed_teams")
    private List<String> authedTeams;
    .
    .
    .
}

private class Event {

  private String type;
  .
  .
  .

  @SerializedName("event_ts")
  private string eventTs;

  @SerializedName("channel_type")
  private String channelType;
}

答案 2 :(得分:0)

对于GSON库,您可以创建一个与JSON结构相同的类包,为此,您可以使用http://www.jsonschema2pojo.org/并且可以使用 .fromJson()方法,以将JSON内容传输到容器类。

另一件事是,您可以将JSON传输到Java Map类,而不必创建自己的类。

但是,如果您想更简单一些,就我的喜好而言,还有其他库,最容易使用的是JSON路径:https://github.com/json-path/JsonPath