【问题标题】:Youtube API - How to retrieve json instead of xml?Youtube API - 如何检索 json 而不是 xml?
【发布时间】:2013-01-04 03:13:25
【问题描述】:

我想使用 JSON 格式而不是 XML 从 youtube api 加载数据。这是我的消息来源:

- (void) getFavoriteList{


    NSString* rawUrl = CONNECTION_GET_FAVORITE_LIST;
    NSURL *url = [NSURL URLWithString:rawUrl];
    ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];

   //create request header
    AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    [request addRequestHeader:@"Authorization" value:[@" Bearer "  stringByAppendingString:[appDelegate userInfo].accessTokenYoutube ]];
    [request addRequestHeader:@"X-GData-Key" value:[NSString stringWithFormat:@"key=%@",YOUTUBE_DEVELOPER_KEY]];
    [request addRequestHeader:@"alt" value:@"json"];
    [request addRequestHeader:@"v" value:@"2"];
    [request setRequestMethod:@"GET"];
    [request startAsynchronous];

    //register callback blocks.
    [request setCompletionBlock:^{
        NSString* respondMessage = [request responseString];
        ParseData *parseData = [[ParseData alloc] init];
        NSMutableArray *arrPlaylistVideos = [parseData allocVideo:respondMessage];
        [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_GET_FAVORITE_VIDEOS_SUCCESS object:arrPlaylistVideos];
    }];
    [request setFailedBlock:^{
        NSString* respondMessage = [[request error] localizedDescription];
        [[NSNotificationCenter defaultCenter]postNotificationName:NOTIFICATION_GET_FAVORITE_VIDEOS_FAILED object:respondMessage];
    }];

    [request release];
}

我设置了 alt = json 但 Youtube 总是返回 xml 数据。我在这里犯了什么错误吗?

注意:请不要告诉我应该使用 Gdata-ObjectiveC-Client。它非常非常复杂,我讨厌使用它。

谢谢。

【问题讨论】:

    标签: objective-c youtube-api


    【解决方案1】:

    我相信您必须在请求 URL 中添加 alt=json 作为查询字符串,而不是在请求标头中。

    【讨论】:

      【解决方案2】:

      您可以通过 NSXMLParser 解析 xml。或者你可以很容易地找到一个 xml 到 JSON 库。我认为this post 会有所帮助。

      【讨论】:

        猜你喜欢
        • 2015-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多