1

last.fm Web サービスを使用してアルバムを検索する iOS アプリを作成しています。次のコードは、返された json を NSArray に変換します。

results = (NSArray *)[[[json valueForKey:@"results"] valueForKey:@"albummatches"] valueForKey:@"album"];

これは、検索用語が結果を返す場合は正常に機能しますが、結果が返されない場合はアプリケーションがクラッシュします。これは、結果が見つからない場合、json にキー「album」が存在しないためです。

「アルバム」キーがjsonに存在するかどうかをアプリケーションに確認させるにはどうすればよいですか?

編集: 結果がない場合に返されるサンプル json。検索文字列は「asdfgasdfg」でした。Albummatches キーの下にある文字の戻りに注意してください。

2013-03-12 14:08:00.889 MyMusicLibrary[387:1a03] {
results =     {
    "@attr" =         {
        for = asdfgasdfg;
    };
    albummatches = "\n";
    "opensearch:Query" =         {
        "#text" = "";
        role = request;
        searchTerms = asdfgasdfg;
        startPage = 1;
    };
    "opensearch:itemsPerPage" = 50;
    "opensearch:startIndex" = 0;
    "opensearch:totalResults" = 0;
};
}

編集 2: 結果が返されたときのサンプル json:

2013-03-12 14:19:42.769 MyMusicLibrary[880:1a03] {
results =     {
    "@attr" =         {
        for = coldplay;
    };
    albummatches =         {
        album =             (
                            {
                artist = Coldplay;
                id = 1416655;
                image =                     (
                                            {
                        "#text" = "http://userserve-ak.last.fm/serve/34s/85845017.png";
                        size = small;
                    },
                                            {
                        "#text" = "http://userserve-ak.last.fm/serve/64s/85845017.png";
                        size = medium;
                    },
                                            {
                        "#text" = "http://userserve-ak.last.fm/serve/126/85845017.png";
                        size = large;
                    },
                                            {
                        "#text" = "http://userserve-ak.last.fm/serve/300x300/85845017.png";
                        size = extralarge;
                    }
                );
                mbid = "c67be398-9417-4c22-bc13-d6158029ae21";
                name = "A Rush of Blood to the Head";
                streamable = 1;
                url = "http://www.last.fm/music/Coldplay/A+Rush+of+Blood+to+the+Head";
            },
                            {
                artist = Coldplay;
                id = 1984;
                image =                     (
                                            {
                        "#text" = "http://userserve-ak.last.fm/serve/34s/87203265.png";
                        size = small;
                    },
                                            {
                        "#text" = "http://userserve-ak.last.fm/serve/64s/87203265.png";
                        size = medium;
                    },
                                            {
                        "#text" = "http://userserve-ak.last.fm/serve/126/87203265.png";
                        size = large;
                    },
                                            {
                        "#text" = "http://userserve-ak.last.fm/serve/300x300/87203265.png";
                        size = extralarge;
                    }
                );
                mbid = "8e602038-c0f2-3c2d-9068-a1a3daca493d";
                name = Parachutes;
                streamable = 1;
                url = "http://www.last.fm/music/Coldplay/Parachutes";
            }
        );
    };
    "opensearch:Query" =         {
        "#text" = "";
        role = request;
        searchTerms = coldplay;
        startPage = 1;
    };
    "opensearch:itemsPerPage" = 2;
    "opensearch:startIndex" = 0;
    "opensearch:totalResults" = 375;
};
}
4

3 に答える 3

1

これには2つの方法があります

  1. が実際に正しい結果数であると仮定すると、"opensearch:totalResults"この値を使用して結果があるかどうかを判断し、次に使用できます

    NSArray *results = nil;
    
    if ([[json valueForKeyPath:@"results.opensearch:totalResults"] intValue] > 0) {
      results = [json valueForKeyPath:@"results.albummatches.album"];
    }
    
  2. 処理する前に、文字列または配列があるかどうかを確認してください

    id albummatches = [json valueForKeyPath:@"results.albummatches"];
    
    // is this a dictionary or a string?
    // You can either ask if it implements the interface or ask what class it is. I prefer the first
    BOOL isDictionary = [albummatches respondsToSelector:@selector(objectForKey:)];
    
    // or
    BOOL isDictionary = [albummatches isKindOfClass:[NSDictionary class]];
    
    if (isDictionary) {
      results = [albummatches objectForKey:@"album"];
    }
    
于 2013-03-12T14:35:52.670 に答える
0

誰かが以前に本当に良い答えを投稿しましたが、その後削除されました。しかし、これは大いに役立ちました。以下のコードを取得するために少し変更しました。これで動作します。

            NSDictionary *resultsDict = [json valueForKey:@"results"];
        if (resultsDict != nil) {
            //Checks to see if any results are returned
            NSDictionary *albumMatchesDict = [resultsDict valueForKey:@"albummatches"];
            NSString *albumMatchesString = [resultsDict valueForKey:@"albummatches"];
            NSCharacterSet *set = [NSCharacterSet whitespaceCharacterSet];
            albumMatchesString = [[albumMatchesString description] stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];

            if ([[albumMatchesString stringByTrimmingCharactersInSet:set] length] != 0) {
                results = (NSArray *) [albumMatchesDict valueForKey:@"album"];
            }
        }
于 2013-03-11T20:19:08.820 に答える
0

小切手:

if([[json[@"result"][@"albummatches"] allKeys] containsObject:@"album"]==YES){

    //do your work here
}
于 2013-03-11T16:07:37.010 に答える