0

我正在玩 SoundCloud iOS 教程,并且一直在更改 API 调用以获取不同的信息。

但是,当我尝试对 URL 执行 GET 请求时,我似乎无法为调用 API 获取任何 JSON https://api.soundcloud.com/me/activities/tracks/affiliated.json。GET 请求在 API 控制台中工作。其他 GET 请求也可以工作,所以我认为这绝对是我在 Xcode 中请求信息的方式。

这是程序的“金钱”部分——获取数据、解析、创建一个新的视图控制器,该控制器具有一个数组作为响应的属性。

    SCRequestResponseHandler handler;
    handler = ^(NSURLResponse *response, NSData *data, NSError *error) {
        NSError *jsonError = nil;
        NSJSONSerialization *jsonResponse = [NSJSONSerialization
                                             JSONObjectWithData:data
                                                        options:0
                                                          error:&jsonError];
        if (!jsonError && [jsonResponse isKindOfClass:[NSArray class]]) {
            UIStoryboard *sb = self.storyboard;
            SCTTrackListViewController *trackListVC;
            trackListVC = [sb instantiateViewControllerWithIdentifier:@"TLVC"];
            trackListVC.tracks = (NSArray *)jsonResponse;

            // Am I getting JSON back for tracks/affiliated.json ?
            NSLog(@"json %@",(NSArray *)jsonResponse);

            [self presentViewController:trackListVC
                               animated:YES
                             completion:nil];
        }
    };

    NSString *resourceURL = @"https://api.soundcloud.com/me/activities/tracks/affiliated.json";

    [SCRequest performMethod:SCRequestMethodGET
                  onResource:[NSURL URLWithString:resourceURL]
             usingParameters:nil
                 withAccount:account
      sendingProgressHandler:nil
             responseHandler:handler];
}

如果我使用NSString *resourceURL = @"https://api.soundcloud.com/me/tracks.json";or NSString *resourceURL = @"https://api.soundcloud.com/me/favorites.json"; 然后我得到一个以 JSON 格式返回的曲目列表。如果我使用代码示例中的那个,我没有得到任何响应 - 完全沉默。

我得到的 JSON 响应https://api.soundcloud.com/me/favorites.json的形式是

[
   {
    "kind": "track",
    "id": 112547469,
    "created_at": "2013/09/26 06:50:06 +0000",
    "user_id": 294371,
    "duration": 4499751,
    "commentable": true,
    "state": "finished",
    "original_content_size": 143977742,
    "sharing": "public",
    "tag_list": "Funk Disco",
    "permalink": "the-boogie-down",
    "streamable": true,
    "embeddable_by": "all",
    "downloadable": false,
    "purchase_url": null,
    "label_id": null,
    "purchase_title": null,
    "genre": "Boogie",
    "title": "The Boogie Down",
    "description": "1.\tWar - The World Is A Ghetto (Suonho Edit)\r\n\r\n2. \tWhiskey Barons - The Same Love\r\n\r\n3.\tRayko - Broadway\r\n\r\n4.\tNicholas - Talking About Love\r\n\r\n5.\tBen E. King - Supernatural Thing (Fingerman Edit)\r\n\r\n6.\tGazeebo - Scaredy Cat\r\n\r\n7.\tFingerman - Fat Like You Know\r\n\r\n8.\tWillie Beaver - Party Time (Karim Edit)\r\n\r\n9.\tDeadly Sins - I Can Feel It\r\n\r\n10.\tFingerman - Tootin\r\n\r\n11.\tRocco Raimundo - Give Me Your Love\r\n\r\n12.\tLuther Vandross - Never Too Much\r\n\r\n13.\tKool & The Gang - Get Down On It (Rocco Raimundo Edit)",
    "label_name": "",
    "release": "",
    "track_type": "",
    "key_signature": "",
    "isrc": "",
    "video_url": null,
    "bpm": null,
    "release_year": null,
    "release_month": null,
    "release_day": null,
    "original_format": "mp3",
    "license": "all-rights-reserved",
    "uri": "https://api.soundcloud.com/tracks/112547469",
    "user":  {
      "id": 294371,
      "kind": "user",
      "permalink": "web_d",
      "username": "webd",
      "uri": "https://api.soundcloud.com/users/294371",
      "permalink_url": "http://soundcloud.com/web_d",
      "avatar_url": "https://i1.sndcdn.com/avatars-000011907556-1k1cgw-large.jpg?3eddc42"
    },
    "user_playback_count": 1,
    "user_favorite": true,
    "permalink_url": "http://soundcloud.com/web_d/the-boogie-down",
    "artwork_url": "https://i1.sndcdn.com/artworks-000058671332-jzz9uc-large.jpg?3eddc42",
    "waveform_url": "https://w1.sndcdn.com/qA5zJCmDqO46_m.png",
    "stream_url": "https://api.soundcloud.com/tracks/112547469/stream",
    "playback_count": 69,
    "download_count": 0,
    "favoritings_count": 1,
    "comment_count": 0,
    "attachments_uri": "https://api.soundcloud.com/tracks/112547469/attachments"
  },

而响应https://api.soundcloud.com/me/activities/tracks/affiliated.json去:

{
  "collection":  [
     {
      "type": "track",
      "created_at": "2013/10/08 20:14:16 +0000",
      "origin":  {
        "kind": "track",
        "id": 114421587,
        "created_at": "2013/10/08 18:20:21 +0000",
        "user_id": 144598,
        "duration": 340616,
        "commentable": true,
        "state": "finished",
        "original_content_size": 13624479,
        "sharing": "public",
        "tag_list": "",
        "permalink": "faden-away",
        "streamable": true,
        "embeddable_by": "all",
        "downloadable": false,
        "purchase_url": null,
        "label_id": null,
        "purchase_title": null,
        "genre": "Funk",
        "title": "Faden Away",
        "description": "",
        "label_name": "",
        "release": "",
        "track_type": "",
        "key_signature": "",
        "isrc": "",
        "video_url": null,
        "bpm": null,
        "release_year": null,
        "release_month": null,
        "release_day": null,
        "original_format": "mp3",
        "license": "all-rights-reserved",
        "uri": "https://api.soundcloud.com/tracks/114421587",
        "user":  {
          "id": 144598,
          "kind": "user",
          "permalink": "stonesthrow",
          "username": "Stones Throw Records",
          "uri": "https://api.soundcloud.com/users/144598",
          "permalink_url": "http://soundcloud.com/stonesthrow",
          "avatar_url": "https://i1.sndcdn.com/avatars-000001771161-2x04bn-large.jpg?3eddc42"
        },
        "user_playback_count": 1,
        "user_favorite": false,
        "permalink_url": "http://soundcloud.com/stonesthrow/faden-away",
        "artwork_url": "https://i1.sndcdn.com/artworks-000059640743-qtexag-large.jpg?3eddc42",
        "waveform_url": "https://w1.sndcdn.com/NBZ15f1BZ4cV_m.png",
        "stream_url": "https://api.soundcloud.com/tracks/114421587/stream",
        "playback_count": 29756,
        "download_count": 0,
        "favoritings_count": 1587,
        "comment_count": 138,
        "attachments_uri": "https://api.soundcloud.com/tracks/114421587/attachments",
        "sharing_note":  {
          "text": "",
          "created_at": "2013/10/08 20:14:16 +0000"
        }
      },
      "tags": "affiliated"
    },
     {
      "type": "track",
      "created_at": "2013/10/08 14:26:00 +0000",
      "origin":  {
        "kind": "track",
        "id": 112820793,
        "created_at": "2013/09/27 21:39:16 +0000",
        "user_id": 1520490,
        "duration": 290926,
        "commentable": true,
        "state": "finished",
        "original_content_size": 12579855,
        "sharing": "public",
        "tag_list": ""MUSEUM OF LOVE" "DFA RECORDS" "PAT MAHONEY" "LCD SOUNDSYSTEM" "THE JUAN MACLEAN" JEE DAY DFA",
        "permalink": "museum-of-love-monotronic",
        "streamable": true,
        "embeddable_by": "all",
        "downloadable": false,
        "purchase_url": "https://itunes.apple.com/album/monotronic-single/id711563768",
        "label_id": null,
        "purchase_title": "DIGITAL SINGLE",
        "genre": "",
        "title": "Museum of Love - Monotronic",
        "description": "Museum Of Love\n"Monotronic"\n\nDFA2390\n\nfirst single "Down South" available here: http://store.dfarecords.com/products/dfa2389",
        "label_name": "DFA Records",
        "release": "DFA2390",
        "track_type": "original",
        "key_signature": "",
        "isrc": "",
        "video_url": "http://www.youtube.com/watch?v=K2E6oK7tN5w",
        "bpm": null,
        "release_year": 2013,
        "release_month": 10,
        "release_day": 8,
        "original_format": "mp3",
        "license": "all-rights-reserved",
        "uri": "https://api.soundcloud.com/tracks/112820793",
        "user":  {
          "id": 1520490,
          "kind": "user",
          "permalink": "dfa-records",
          "username": "DFA Records",
          "uri": "https://api.soundcloud.com/users/1520490",
          "permalink_url": "http://soundcloud.com/dfa-records",
          "avatar_url": "https://i1.sndcdn.com/avatars-000002067008-el39h6-large.jpg?3eddc42"
        },
        "user_playback_count": 1,
        "user_favorite": false,
        "permalink_url": "http://soundcloud.com/dfa-records/museum-of-love-monotronic",
        "artwork_url": "https://i1.sndcdn.com/artworks-000058798982-ogzv3j-large.jpg?3eddc42",
        "waveform_url": "https://w1.sndcdn.com/0m0pwUhjvenc_m.png",
        "stream_url": "https://api.soundcloud.com/tracks/112820793/stream",
        "playback_count": 10047,
        "download_count": 0,
        "favoritings_count": 309,
        "comment_count": 18,
        "attachments_uri": "https://api.soundcloud.com/tracks/112820793/attachments",
        "sharing_note":  {
          "text": "",
          "created_at": "2013/09/27 21:39:16 +0000"
        }
      },
      "tags": "affiliated"
    },

我正在尝试将响应存储在 NSArray 中,我认为这可能是问题所在......有人能指出我正确的方向来获取affiliated.json 的 JSON 吗?第二个 JSON 的响应是字典吗?我是否需要以不同的方式存储该响应?我以为我可以将字典存储在 NSArray 中...

4

1 回答 1

0

我什么都没回来的原因是因为这条线:NSLog(@"json %@",(NSArray *)jsonResponse);我在哪里转换jsonResponse为 NSArray。这在第一个实例中有效,因为响应是一个数组,但在第二个实例中,响应是一个字典。现在想想就很明显了。我把那条线改成了(NSDictionary *)jsonResponse,一切都很好。

在我收到 JSON 数据后,这也是尝试不同方式挖掘数据中嵌套信息的好方法。例如,我创建了一个字典来保存 jsonResponse。我可以通过以下几种方式获取所需密钥的信息:

dictionary[@"user"][@"avatar_url]将获得avatar_url本身嵌套在user.

我也可以通过调用来做到这一点[dictionary valueForKeyPath:@"user.avatar_url"];

于 2013-10-14T03:08:24.390 回答