使用 Gson 解析包含动态键的 JSON 数据到 POJO

使用 Gson 解析包含动态键的 JSON 数据到 POJO

本文档旨在帮助开发者理解如何使用 Gson 库解析包含动态键的 json 数据,并将其映射到 Java POJO (Plain Old Java Object) 类中。我们将通过一个股票行情数据的例子,详细介绍如何正确地定义 POJO 类结构,以及如何使用 Gson 进行反序列化,解决retrofit2返回NULL的问题。

理解 JSON 结构与 POJO 设计

在处理包含动态键的 JSON 数据时,关键在于理解数据的结构,并设计合适的 POJO 类来映射它。例如,考虑以下 JSON 结构:

{     "Meta Data": {         "1. Information": "intraday (5min) open, high, low, close prices and volume",         "2. Symbol": "IBM",         "3. Last Refreshed": "2022-10-26 19:40:00",         "4. Interval": "5min",         "5. Output Size": "Compact",         "6. Time Zone": "US/Eastern"     },     "Time Series (5min)": {         "2022-10-26 19:40:00": {             "1. open": "135.0600",             "2. high": "135.0700",             "3. low": "135.0400",             "4. close": "135.0700",             "5. volume": "500"         },         "2022-10-26 19:05:00": {             "1. open": "135.3500",             "2. high": "135.3500",             "3. low": "135.3500",             "4. close": "135.3500",             "5. volume": "106"         }     } }

在这个例子中,”Time Series (5min)” 下的键是动态的,代表不同的时间戳。我们需要使用 map 来映射这种结构,其中 String 是时间戳,date 是包含股票数据的 POJO 类。

POJO 类定义

以下是对应的 POJO 类定义:

import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName;  import java.util.Map;  public class DailyQuote {      @SerializedName("Meta Data")     @Expose     private MetaData metaData;      @SerializedName("Time Series (5min)")     @Expose     private Map<String, Date> timeSeries; // 修改为 Map<String, Date>      public DailyQuote() {     }      public DailyQuote(MetaData metaData, Map<String, Date> timeSeries) {         this.metaData = metaData;         this.timeSeries = timeSeries;     }      public MetaData getMetaData() {         return metaData;     }      public void setMetaData(MetaData metaData) {         this.metaData = metaData;     }      public Map<String, Date> getTimeSeries() {         return timeSeries;     }      public void setTimeSeries(Map<String, Date> timeSeries) {         this.timeSeries = timeSeries;     } }  public class MetaData {      @SerializedName("1. Information")     @Expose     private String _1Information;     @SerializedName("2. Symbol")     @Expose     private String _2Symbol;     @SerializedName("3. Last Refreshed")     @Expose     private String _3LastRefreshed;     @SerializedName("4. Interval")     @Expose     private String _4Interval;     @SerializedName("5. Output Size")     @Expose     private String _5OutputSize;     @SerializedName("6. Time Zone")     @Expose     private String _6TimeZone;      public MetaData() {     }      public MetaData(String _1Information, String _2Symbol, String _3LastRefreshed, String _4Interval, String _5OutputSize, String _6TimeZone) {         this._1Information = _1Information;         this._2Symbol = _2Symbol;         this._3LastRefreshed = _3LastRefreshed;         this._4Interval = _4Interval;         this._5OutputSize = _5OutputSize;         this._6TimeZone = _6TimeZone;     }      public String get1Information() {         return _1Information;     }      public void set1Information(String _1Information) {         this._1Information = _1Information;     }      public String get2Symbol() {         return _2Symbol;     }      public void set2Symbol(String _2Symbol) {         this._2Symbol = _2Symbol;     }      public String get3LastRefreshed() {         return _3LastRefreshed;     }      public void set3LastRefreshed(String _3LastRefreshed) {         this._3LastRefreshed = _3LastRefreshed;     }      public String get4Interval() {         return _4Interval;     }      public void set4Interval(String _4Interval) {         this._4Interval = _4Interval;     }      public String get5OutputSize() {         return _5OutputSize;     }      public void set5OutputSize(String _5OutputSize) {         this._5OutputSize = _5OutputSize;     }      public String get6TimeZone() {         return _6TimeZone;     }      public void set6TimeZone(String _6TimeZone) {         this._6TimeZone = _6TimeZone;     } }  public class Date {      @SerializedName("1. open")     @Expose     private String _1Open;     @SerializedName("2. high")     @Expose     private String _2High;     @SerializedName("3. low")     @Expose     private String _3Low;     @SerializedName("4. close")     @Expose     private String _4Close;     @SerializedName("5. volume")     @Expose     private String _5Volume;      public Date() {     }      public Date(String _1Open, String _2High, String _3Low, String _4Close, String _5Volume) {         this._1Open = _1Open;         this._2High = _2High;         this._3Low = _3Low;         this._4Close = _4Close;         this._5Volume = _5Volume;     }      public String get1Open() {         return _1Open;     }      public void set1Open(String _1Open) {         this._1Open = _1Open;     }      public String get2High() {         return _2High;     }      public void set2High(String _2High) {         this._2High = _2High;     }      public String get3Low() {         return _3Low;     }      public void set3Low(String _3Low) {         this._3Low = _3Low;     }      public String get4Close() {         return _4Close;     }      public void set4Close(String _4Close) {         this._4Close = _4Close;     }      public String get5Volume() {         return _5Volume;     }      public void set5Volume(String _5Volume) {         this._5Volume = _5Volume;     }      @Override     public String toString() {         return "List:{" +                 "Open='" + get1Open() + ''' +                 ", High='" + get2High() + ''' +                 ", Low='" + get3Low() + ''' +                 ", Close='" + get4Close() + ''' +                 ", Volume='" + get5Volume();     } }

注意 DailyQuote 类中 timeSeries 字段的类型是 Map,而不是 TimeSeries。 TimeSeries 类不再需要,直接使用 Map 即可。

使用 Gson 反序列化 JSON

使用 Gson 反序列化 JSON 数据非常简单:

import com.google.gson.Gson;  public class Main {     public static void main(String[] args) {         String json = "{n" +                 "    "Meta Data": {n" +                 "        "1. Information": "Intraday (5min) open, high, low, close prices and volume",n" +                 "        "2. Symbol": "IBM",n" +                 "        "3. Last Refreshed": "2022-10-26 19:40:00",n" +                 "        "4. Interval": "5min",n" +                 "        "5. Output Size": "Compact",n" +                 "        "6. Time Zone": "US/Eastern"n" +                 "    },n" +                 "    "Time Series (5min)": {n" +                 "        "2022-10-26 19:40:00": {n" +                 "            "1. open": "135.0600",n" +                 "            "2. high": "135.0700",n" +                 "            "3. low": "135.0400",n" +                 "            "4. close": "135.0700",n" +                 "            "5. volume": "500"n" +                 "        },n" +                 "        "2022-10-26 19:05:00": {n" +                 "            "1. open": "135.3500",n" +                 "            "2. high": "135.3500",n" +                 "            "3. low": "135.3500",n" +                 "            "4. close": "135.3500",n" +                 "            "5. volume": "106"n" +                 "        }n" +                 "    }n" +                 "}";          Gson gson = new Gson();         DailyQuote dailyQuote = gson.fromJson(json, DailyQuote.class);          System.out.println("Meta Data: " + dailyQuote.getMetaData().get2Symbol());         System.out.println("Time Series: " + dailyQuote.getTimeSeries().toString());     } }

这段代码首先创建了一个 Gson 实例,然后使用 fromJson() 方法将 JSON 字符串转换为 DailyQuote 对象

Retrofit 集成

在 Retrofit 中使用 Gson,你需要确保 Gson Converter Factory 被正确配置。以下是一个简单的 Retrofit 接口定义:

import retrofit2.Call; import retrofit2.http.GET;  public interface ApiService {     @GET("your_api_endpoint")     Call<DailyQuote> getDailyQuote(); }

确保你的 Retrofit 客户端配置中包含了 Gson Converter Factory:

import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory;  public class RetrofitClient {      private static Retrofit retrofit;     private static final String BASE_URL = "your_base_url";      public static Retrofit getRetrofitInstance() {         if (retrofit == null) {             retrofit = new Retrofit.Builder()                     .baseUrl(BASE_URL)                     .addConverterFactory(GsonConverterFactory.create())                     .build();         }         return retrofit;     } }

然后,你可以像这样使用 Retrofit 获取数据:

import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response;  public class NetworkCall {      public static void main(String[] args) {         ApiService apiService = RetrofitClient.getRetrofitInstance().create(ApiService.class);         Call<DailyQuote> call = apiService.getDailyQuote();          call.enqueue(new Callback<DailyQuote>() {             @Override             public void onResponse(Call<DailyQuote> call, Response<DailyQuote> response) {                 if (response.isSuccessful()) {                     DailyQuote dailyQuote = response.body();                     System.out.println("Meta Data: " + dailyQuote.getMetaData().get2Symbol());                     System.out.println("Time Series: " + dailyQuote.getTimeSeries().toString());                 } else {                     System.out.println("Request failed: " + response.message());                 }             }              @Override             public void onFailure(Call<DailyQuote> call, Throwable t) {                 System.out.println("Request failed: " + t.getMessage());             }         });     } }

注意事项

  • 字段命名: 确保 POJO 类的字段名与 JSON 中的键名一致,或者使用 @SerializedName 注解来指定映射关系。
  • 数据类型 选择合适的数据类型来映射 JSON 中的值。例如,数字可以使用 int、double 或 String,布尔值可以使用 Boolean,数组可以使用 List。
  • Gson 配置: 可以自定义 Gson 实例,例如设置日期格式、处理 null 值等。
  • 异常处理: 在反序列化过程中,可能会出现异常,例如 JSON 格式错误、数据类型不匹配等。需要进行适当的异常处理。

总结

通过本文,你学习了如何使用 Gson 解析包含动态键的 JSON 数据,并将其映射到 POJO 类中。关键在于理解 JSON 结构,并使用 Map 或自定义 POJO 类来映射动态键。结合 Retrofit,你可以轻松地从 API 获取数据,并将其转换为 Java 对象进行处理。 记住,良好的 POJO 设计和适当的 Gson 配置是成功解析 JSON 数据的关键。

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享