在Android中使用翻新时使用带有动态键的JSON [英] JSON with Dynamic Keys while using Retrofit in Android

查看:88
本文介绍了在Android中使用翻新时使用带有动态键的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是在Android中使用Retrofit的新手.我被困在向REST Api发出请求且响应具有动态键的位置.谁能告诉我解析这样的JSON的最佳方法是什么.

I'm new in using Retrofit with Android. I'm stuck at a point where I make a request to the REST Api and the response has a dynamic keys. Can anyone tell me what is the best way to parse the JSON something like this.

{
  "Meta Data": {
    "1. Information": "Intraday (1min) prices and volumes",
    "2. Symbol": "MSFT",
    "3. Last Refreshed": "2017-12-01 16:00:00",
    "4. Interval": "1min",
    "5. Output Size": "Compact",
    "6. Time Zone": "US/Eastern"
  },
  "Time Series (1min)": {
    "2017-12-01 16:00:00": {
      "1. open": "84.2000",
      "2. high": "84.2600",
      "3. low": "84.1800",
      "4. close": "84.2000",
      "5. volume": "3311341"
    },
    "2017-12-01 15:59:00": {
      "1. open": "84.2500",
      "2. high": "84.2600",
      "3. low": "84.2000",
      "4. close": "84.2000",
      "5. volume": "175169"
    },
    "2017-12-01 15:58:00": {
      "1. open": "84.2800",
      "2. high": "84.3000",
      "3. low": "84.2400",
      "4. close": "84.2550",
      "5. volume": "139520"
    }

  }
}

推荐答案

如果您使用的是 Retrofit ,则可以直接将其解析为某些模型,例如 TestModel 根据您的要求调用类似这样的内容.

If you are using the Retrofit then you can directly parse this into some model let's say TestModel based on your requirement call something like this.

@GET("your endpoint")
Call<TestModel> getTestData();

在这里您将获得数据

public void getTestData(){
    mApiServiceNetwork.getNetworkService(null,WebConstants.API_ENDPOINT)
            .getTestData()
            .enqueue(new Callback<TestModel>() {
                @Override
                public void onResponse(final Call<TestModel> call, final Response<TestModel> response) {
                    if (response.code()==200){
                        //handle the response
                        TestModel testModel = response.body();
                    }else{
                        //handle the error
                    }
                }

                @Override
                public void onFailure(final Call<TestModel> call, final Throwable t) {

                }
            });
}

如果您要通过 JSON 对象获取数据,请这样做.

if you are getting data in JSON object then do like this.

 private TestModel getTestModel(JsonObject jsonObject){
        Gson gson = new Gson();
        TestModel  testModel = gson.fromJson(jsonObject,TestModel.class);
        return testModel;
    }

如果要获取String中的数据,然后解析为Json对象,则调用上述方法,这里是将字符串转换为json对象的方法.

and if you are getting data in String then parse into Json object then call above method here is how you can convert string into json object.

JsonObject jsonParser = new JsonParser().parse(json).getAsJsonObject();

最重要的是,在两种情况下,您的TestModel都应如下所示.

and most important your TestModel should be like this below for both the cases.

public class TestModel {
@SerializedName("Meta Data")
@Expose
private MetaModel mMetaModel;

public MetaModel getMetaModel() {
    return mMetaModel;
}

public void setMetaModel(final MetaModel metaModel) {
    mMetaModel = metaModel;
}

public Map<String, TimeModel> getTimeModel() {
    return mTimeModel;
}

public void setTimeModel(final Map<String, TimeModel> timeModel) {
    mTimeModel = timeModel;
}

@SerializedName("Time Series (1min)")

@Expose
private Map<String,TimeModel> mTimeModel;

public static class MetaModel{

    @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 String get_1Information() {
        return _1Information;
    }

    public void set_1Information(final String _1Information) {
        this._1Information = _1Information;
    }

    public String get_2Symbol() {
        return _2Symbol;
    }

    public void set_2Symbol(final String _2Symbol) {
        this._2Symbol = _2Symbol;
    }

    public String get_3LastRefreshed() {
        return _3LastRefreshed;
    }

    public void set_3LastRefreshed(final String _3LastRefreshed) {
        this._3LastRefreshed = _3LastRefreshed;
    }

    public String get_4Interval() {
        return _4Interval;
    }

    public void set_4Interval(final String _4Interval) {
        this._4Interval = _4Interval;
    }

    public String get_5OutputSize() {
        return _5OutputSize;
    }

    public void set_5OutputSize(final String _5OutputSize) {
        this._5OutputSize = _5OutputSize;
    }

    public String get_6TimeZone() {
        return _6TimeZone;
    }

    public void set_6TimeZone(final String _6TimeZone) {
        this._6TimeZone = _6TimeZone;
    }
}
public static class TimeModel{
    @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 String get_1Open() {
        return _1Open;
    }

    public void set_1Open(final String _1Open) {
        this._1Open = _1Open;
    }

    public String get_2High() {
        return _2High;
    }

    public void set_2High(final String _2High) {
        this._2High = _2High;
    }

    public String get_3Low() {
        return _3Low;
    }

    public void set_3Low(final String _3Low) {
        this._3Low = _3Low;
    }

    public String get_4Close() {
        return _4Close;
    }

    public void set_4Close(final String _4Close) {
        this._4Close = _4Close;
    }

    public String get_5Volume() {
        return _5Volume;
    }

    public void set_5Volume(final String _5Volume) {
        this._5Volume = _5Volume;
    }
}

}

关键是如果我们对Map<K,V>进行改造,如果模型就像JSON中一样,它将自动将其解析为模型.

the key point is if we give retrofit with Map<K,V> it will parse this into model automatically if the model is just like in JSON.

这篇关于在Android中使用翻新时使用带有动态键的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆