【问题标题】:Decode GSON Model Subclass解码 GSON 模型子类
【发布时间】:2021-11-05 11:23:47
【问题描述】:

我对 android 和 GSON 库非常陌生,但在尝试获取 GSON 模型子类时遇到了问题。

我使用预定义的 OKhttp Profiler 构建器创建了一个带有子类 Values 的 GSON 对象 Indicators 并获取方法:

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

public class Indicators {
    @SerializedName("indicator")
    public Indicator indicator;

    public static class Values {
        @SerializedName("value")
        private static double value;
        @SerializedName("datetime")
        private static String datetime;
        @SerializedName("datetime_utc")
        private static String datetimeUtc;
        @SerializedName("tz_time")
        private static String tzTime;
        @SerializedName("geo_id")
        private static int geoId;
        @SerializedName("geo_name")
        private static String geoName;


        public static double getValue() {
            return value;
        }

        public static String getDatetime() {
            return datetime;
        }

        public static String getDatetimeUtc() {
            return datetimeUtc;
        }

        public static String getTzTime() {
            return tzTime;
        }

        public static int getGeoId() {
            return geoId;
        }

        public static String getGeoName() {
            return geoName;
        }
    }

}

我一直在获取价值:

public class HandelJSON {

    public static void GSONModel(String JSON) {

        Gson gson = new Gson();

        Indicators IndicatorsModel = gson.fromJson(JSON, Indicators.class);

        // ... Here I am missed

    }

}

如何调用子类子方法?我试过IndicatorsModel.Values.getValue();,但我得到“预期的类或包”

【问题讨论】:

  • 为什么Values是一个静态类?

标签: java android gson


【解决方案1】:

序列化不适用于静态字段,您的代码中存在逻辑错误!

我建议您read this article 更好地了解什么是序列化和this article 开始使用 Gson

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多