Android Youtube API JSON-C 使用 GSON 解析

标签 android json youtube gson

我正在尝试开发一个 Android 应用程序来检索特定用户的视频,并且我想使用 JSON-C 响应格式来保持较小的大小。一旦我收到我的回复,格式如下:

{
"apiVersion":"2.1",
"data":{
    "updated":"2012-12-02T14:40:07.424Z",
    "totalItems":175,
    "startIndex":1,
    "itemsPerPage":25,
    "items":[
        {
            "id":"rnzJ5x3sWyk",
            "uploaded":"2012-12-01T20:19:07.000Z",
            "updated":"2012-12-01T20:19:07.000Z",
            "uploader":"user",
            "category":"News",
            "title":"L'ALLUVIONE HA DISTRUTTO LE ROULOTTE DELLE VACANZE",
            "description":"Ad Albinia,in un rimessaggio per roulotte, il proprietario, i volontari e i vacanzieri che sono soliti passare l'estate qui, si sono rimboccati le maniche per ripulire dal fango le tante \"case delle vacanze\" distrutte dall'alluvione",
            "thumbnail":{
                "sqDefault":"http://i.ytimg.com/vi/rnzJ5x3sWyk/default.jpg",
                "hqDefault":"http://i.ytimg.com/vi/rnzJ5x3sWyk/hqdefault.jpg"
            },
            "player":{
                "default":"http://www.youtube.com/watch?v=rnzJ5x3sWyk&feature=youtube_gdata_player",
                "mobile":"http://m.youtube.com/details?v=rnzJ5x3sWyk"
            },
            "content":{
                "5":"http://www.youtube.com/v/rnzJ5x3sWyk?version=3&f=user_uploads&app=youtube_gdata",
                "1":"rtsp://v1.cache2.c.youtube.com/CigLENy73wIaHwkpW-wd58l8rhMYDSANFEgGUgx1c2VyX3VwbG9hZHMM/0/0/0/video.3gp",
                "6":"rtsp://v6.cache2.c.youtube.com/CigLENy73wIaHwkpW-wd58l8rhMYESARFEgGUgx1c2VyX3VwbG9hZHMM/0/0/0/video.3gp"
            },
            "duration":136,
            "viewCount":2,
            "favoriteCount":0,
            "commentCount":0,
            "accessControl":{
                "comment":"allowed",
                "commentVote":"allowed",
                "videoRespond":"moderated",
                "rate":"allowed",
                "embed":"allowed",
                "list":"allowed",
                "autoPlay":"allowed",
                "syndicate":"allowed"
            }
        },
and others items....

当我设置模型类以反序列化它时,它应该是什么样子 我是否必须包含所有元素,即使是那些我不感兴趣的元素?

 public class SearchResponse {

    public Container data;

  }

指的是

import java.util.ArrayList;

import com.google.gson.annotations.SerializedName;

public class Container {

public ArrayList<Items> results;

@SerializedName("totalItems")
public int totalItems;

@SerializedName("startIndex")
public int startIndex;

@SerializedName("itemsPerPage")
      public int itemsPerPage;
}

对于每个项目:

import com.google.gson.annotations.SerializedName;

public class Items {

@SerializedName("id")
public String id;

@SerializedName("uploaded")
public String uploaded;

@SerializedName("category")
public String category;

@SerializedName("title")
public String title;

@SerializedName("description")
public String description;

public Thumbnail thumbnail;

@SerializedName("duration")
public int duration;


}

以及每个缩略图

import com.google.gson.annotations.SerializedName;

public class Thumbnail {

@SerializedName("sqdefault")
      public String sqdefault;

@SerializedName("hqdefault")
public String hqdefault;

}

我认为“数据”部分正在杀死我。

如果您已经这样做了,有人可以帮忙或给我一些例子吗?

提前致谢

最佳答案

这个:

public ArrayList<Items> results;

需要:

public ArrayList<Items> items;

或者:

@SerializedName("items") public ArrayList<Items> results;

它应该可以正常工作。

不,您不必在 POJO 中包含所有字段。 JSON 中存在的任何您没有的字段都将被静默忽略。

关于Android Youtube API JSON-C 使用 GSON 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13791593/

相关文章:

android dev - 更改ic_launcher但在运行时不出现

json - 在 Ext.data 的上下文中,JsonStore 和 JsonReader 之间的基本区别是什么?

python - 如何使 python 对象 json 序列化?

iframe - iframe播放按钮上的图像z-index,无需js直接通过点击

json - 如何为 v3 YouTube API 上传构建 JSON 中的片段和状态

c# - 我如何知道我的服务器端应用程序正在使用什么引荐来源地址

android - 如何编程按钮将表格布局导出到 Android 应用程序上的图像或 PDF 文件

java - 任务 ':app:packageRelease' 执行失败 无法从存储 "C:\Users\": Keystore was tampered with, 读取 key key0 或密码不正确

android - 在自定义警报对话框中显示 map

javascript - 在 JavaScript 中将 JSON 从文件读取到下拉列表?