如何使用GSON和Typetoken在JSON中获取特定信息?

很抱歉,如果这个问题如此基本,但是我是Android和Retrofit的入门者。我有一个使用Retrofit的请求,该请求返回此JSON:

  "live": {
    "5da49030d3d97f19383a65d0": {
      "publisher": {
        "app": "live","stream": "5da49030d3d97f19383a65d0","video": {
          "codec": "H264","width": 1080,"height": 1920,"profile": "Baseline","level": 4,"fps": 0
        }
      },"subscribers": [
        {
          "app": "live","clientId": "TKEDO7M2",}
      ]
    }
  }
}

此JSON属性"5da49030d3d97f19383a65d0"是一个变量。 我想获取有关正在观看直播的订户的信息。我创建了以下类,以尝试检索信息。

public class Live {
    public Subscriber subscriber;

    public static Live getFromJson(String jsonString) {
        return new Gson().fromJson(jsonString,Live.class);
    }

    public String toJsonString() {
        return new Gson().toJson(this);
    }
}

public class Subscriber extends BaseEntity {
   public Publisher publisher;


        public static Map<String,Publisher> getFromJson(String jsonString) {
        Type empMapType = new TypeToken<Map<String,Publisher>>(){}.getType();
        return new Gson().fromJson(jsonString,empMapType);
    }
}

public class Publisher {
    public String app;
    public String stream;


    public static Live getFromJson(String jsonString) {
        return new Gson().fromJson(jsonString,Live.class);
    }
}

此请求的答案是正确的。我认为问题是解析不正确。

Arbot 回答:如何使用GSON和Typetoken在JSON中获取特定信息?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3088739.html

大家都在问