【问题标题】:YouTube API v3 is not responding in JSON with AlamofireYouTube API v3 在 JSON 中没有响应 Alamofire
【发布时间】:2019-09-06 03:39:00
【问题描述】:

我正在使用 YouTube API v3 来获取有关 Alamofire 查询的搜索结果。这是我的代码:

Alamofire.request(makeURL(query: "https://www.googleapis.com/youtube/v3/search?part=snippet&q=apple&key=AIzaSyAbwfyC36pq1WoaGOkdx7m8cLMh8kMQRGE"), method: .get, parameters: nil).validate().responseJSON { (response) in
    print(response.result.value)     
}

我所期待的: API 响应应采用 JSON 格式。

实际发生的情况: 它不是以 JSON 形式出现,而是以其他形式出现。

如何获得 JSON 格式的响应?

【问题讨论】:

  • 您的网址似乎不正确......
  • 哪部分不正确?
  • { 错误:{ 错误:[ { 域:“youtube.part”,原因:“unknownPart”,消息:“part”,locationType:“parameter”,位置:“part”}],代码:400,消息:“部分”} }
  • 这是您从 youtube API..l 获得的响应,采用 JSON 格式
  • 你是在 Swift 的 Alamofire 上做这个吗?

标签: json swift youtube-api alamofire youtube-data-api


【解决方案1】:
Alamofire.request("https://www.googleapis.com/youtube/v3/search?part=snippet&q=apple&key=AIzaSyAbwfyC36pq1WoaGOkdx7m8cLMh8kMQRGE").responseJSON { (response) in
            print(response)
        }

结果是 JSON 格式

SUCCESS: {
    etag = "\"XpPGQXPnxQJhLgs6enD_n8JR4Qk/XGmvZBQRhfhL_NmuaAj4nOXRYgE\"";
    items =     (
                {
            etag = "\"XpPGQXPnxQJhLgs6enD_n8JR4Qk/3wIsul9XoecWMttT8KgkS-NJVsY\"";
            id =             {
                kind = "youtube#video";
                videoId = hq3yfQnllfQ;
            };
            kind = "youtube#searchResult";
            snippet =             {
                channelId = "UCBnZ16ahKA2DZ_T5W0FPUXg";
                channelTitle = "ChuChu TV Nursery Rhymes & Kids Songs";
                description = "ABC \"Phonics\" song. This animated phonics song will help children learn the sounds of the letters in the English alphabet. This colorful phonics song also ...";
                liveBroadcastContent = none;
                publishedAt = "2014-03-06T20:57:50.000Z";
                thumbnails =                 {
                    default =                     {
                        height = 90;
                        url = "https://i.ytimg.com/vi/hq3yfQnllfQ/default.jpg";
                        width = 120;
                    };
                    high =                     {
                        height = 360;
                        url = "https://i.ytimg.com/vi/hq3yfQnllfQ/hqdefault.jpg";
                        width = 480;
                    };
                    medium =                     {
                        height = 180;
                        url = "https://i.ytimg.com/vi/hq3yfQnllfQ/mqdefault.jpg";
                        width = 320;
                    };
                };
                title = "Phonics Song with TWO Words - A For Apple - ABC Alphabet Songs with Sounds for Children";
            };
        },
                {
            etag = "\"XpPGQXPnxQJhLgs6enD_n8JR4Qk/deVTTMRJ83lNlVBvgZ-IAmhGW74\"";
            id =             {
                channelId = "UCE_M8A5yxnLfW0KghEeajjw";
                kind = "youtube#channel";
            };
            kind = "youtube#searchResult";
            snippet =             {
                channelId = "UCE_M8A5yxnLfW0KghEeajjw";
                channelTitle = Apple;
                description = "Apple revolutionized personal technology with the introduction of the Macintosh in 1984. Today, Apple leads the world in innovation with iPhone, iPad, Mac, ...";
                liveBroadcastContent = none;
                publishedAt = "2005-06-22T05:12:23.000Z";
                thumbnails =                 {
                    default =                     {
                        url = "https://yt3.ggpht.com/-KdgJnz1HIdQ/AAAAAAAAAAI/AAAAAAAAAAA/4vVN7slJqj4/s88-c-k-no-mo-rj-c0xffffff/photo.jpg";
                    };
                    high =                     {
                        url = "https://yt3.ggpht.com/-KdgJnz1HIdQ/AAAAAAAAAAI/AAAAAAAAAAA/4vVN7slJqj4/s800-c-k-no-mo-rj-c0xffffff/photo.jpg";
                    };
                    medium =                     {
                        url = "https://yt3.ggpht.com/-KdgJnz1HIdQ/AAAAAAAAAAI/AAAAAAAAAAA/4vVN7slJqj4/s240-c-k-no-mo-rj-c0xffffff/photo.jpg";
                    };
                };
                title = Apple;
            };
        },
                {
            etag = "\"XpPGQXPnxQJhLgs6enD_n8JR4Qk/VlSh6Sn6qQgT1FNtsvJ9sUkw8ag\"";
            id =             {
                kind = "youtube#video";
                videoId = hXxsTy74Qsg;
            };
            kind = "youtube#searchResult";
            snippet =             {
                channelId = "UCS83hSDCu-fVLvRLQ1aiktQ";
                channelTitle = "Jolly Toy Art";
                description = "Hi guys~ It's Yuna. Let's draw and color Apple & cutlery set ! Don't forget to Subscribe & Like drawing and coloring Glitter closet for kids and toddlers. Thanks For ...";
                liveBroadcastContent = none;
                publishedAt = "2019-02-01T03:00:02.000Z";
                thumbnails =                 {
                    default =                     {
                        height = 90;
                        url = "https://i.ytimg.com/vi/hXxsTy74Qsg/default.jpg";
                        width = 120;
                    };
                    high =                     {
                        height = 360;
                        url = "https://i.ytimg.com/vi/hXxsTy74Qsg/hqdefault.jpg";
                        width = 480;
                    };
                    medium =                     {
                        height = 180;
                        url = "https://i.ytimg.com/vi/hXxsTy74Qsg/mqdefault.jpg";
                        width = 320;
                    };
                };
                title = "Glitter Apple cutlery set coloring and drawing for Kids, Toddlers | Jolly Toy Art \U2606";
            };
        },
                {
            etag = "\"XpPGQXPnxQJhLgs6enD_n8JR4Qk/UnM3fnWWNiSH-CV6p2o8URgd2bI\"";
            id =             {
                kind = "youtube#video";
                videoId = aAUGkuVEbq8;
            };
            kind = "youtube#searchResult";
            snippet =             {
                channelId = UCqSmraORoX9ZE8s7nfEdcuA;
                channelTitle = "Life is Fabulous";
                description = "Apple & Caterpillar Drawing For Kids Thanks for watching!";
                liveBroadcastContent = none;
                publishedAt = "2018-06-08T21:40:21.000Z";
                thumbnails =                 {
                    default =                     {
                        height = 90;
                        url = "https://i.ytimg.com/vi/aAUGkuVEbq8/default.jpg";
                        width = 120;
                    };
                    high =                     {
                        height = 360;
                        url = "https://i.ytimg.com/vi/aAUGkuVEbq8/hqdefault.jpg";
                        width = 480;
                    };
                    medium =                     {
                        height = 180;

                        url = "https://i.ytimg.com/vi/aAUGkuVEbq8/mqdefault.jpg";
                        width = 320;
                    };
                };
                title = "Apple & Caterpillar Drawing For Kids";
            };
        },
                {
            etag = "\"XpPGQXPnxQJhLgs6enD_n8JR4Qk/2jayGhnvbSrxvPCW6IM3M1Zlnws\"";
            id =             {
                kind = "youtube#video";
                videoId = Zc7xmtDkLCE;
            };
            kind = "youtube#searchResult";
            snippet =             {
                channelId = "UCj34AOIMl_k1fF7hcBkD_dw";
                channelTitle = EverythingApplePro;
                description = "Why the iPhone 11 lens is so big! iOS 13 passport support, Mini LED future, original Apple phone prototype & more Apple leaks & news. Last Apple leaks.";
                liveBroadcastContent = none;
                publishedAt = "2019-04-15T17:22:34.000Z";
                thumbnails =                 {

                    default =                     {
                        height = 90;
                        url = "https://i.ytimg.com/vi/Zc7xmtDkLCE/default.jpg";
                        width = 120;
                    };
                    high =                     {
                        height = 360;
                        url = "https://i.ytimg.com/vi/Zc7xmtDkLCE/hqdefault.jpg";
                        width = 480;
                    };
                    medium =                     {
                        height = 180;
                        url = "https://i.ytimg.com/vi/Zc7xmtDkLCE/mqdefault.jpg";
                        width = 320;
                    };
                };
                title = "iPhone 11 Lens Explained + Apples Future Tech!";
            };
        }
    );
    kind = "youtube#searchListResponse";
    nextPageToken = CAUQAA;
    pageInfo =     {
        resultsPerPage = 5;
        totalResults = 1000000;
    };
    regionCode = PK;
}

【讨论】:

  • 所有在线的 json 格式化程序都不识别为 json
  • 兄弟忘记在线 JSON 验证器...这是 JSON 格式...原因是在线验证器无法识别,因为它们不太准确...有时您不需要相信它......继续完成你的任务......
  • 我已经使用过 Youtube API。我向你保证这会奏效。 Alamofire 中的 RESPONSEJSON,它有责任在 JSON 中给出结果。
  • 但是通常我可以下标,我该怎么做呢?
  • 就像你做 json[“items”] 以进一步深入 json 以获取数据时
猜你喜欢
  • 2015-07-21
  • 1970-01-01
  • 2016-08-19
  • 2014-06-29
  • 2014-09-22
  • 2013-01-11
  • 2016-05-23
  • 2014-10-31
  • 1970-01-01
相关资源
最近更新 更多