2013-03-11 96 views
1

我正在編寫使用last.fm Web服務搜索相冊的iOS應用程序。下面的代碼返回的JSON轉換爲一個NSArray:檢查last.fm中是否存在關鍵字返回的json

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

這工作得很好,當一個搜索詞返回結果,但是當返回的應用程序崩潰,沒有結果。這是因爲沒有找到結果時,json中不存在關鍵「專輯」。

如何讓我的應用程序檢查json中是否存在「album」鍵?

編輯:示例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; 
}; 
} 
+0

您假設結果鍵始終存在。但是如果它不是你調用一個無對象的方法 – giorashc 2013-03-11 16:06:43

+0

或者結果總是一個數組,並且試圖將消息發送到不是數組的東西。 – 2013-03-11 16:07:40

+0

你可以提供和NSLog的JSON在哪裏工作? – 2013-03-11 16:15:18

回答

1

有兩種方法,你可以去這個

  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"]; 
    } 
    
+0

選項1已完美工作。非常感謝您花時間幫助我! – Cheese1223 2013-03-12 14:52:48

0

檢查:

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

    //do your work here 
} 
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"]; 
      } 
     } 
+0

這是一個非常糟糕的實現。你能否提供實際的JSON,並且有人能夠告訴你如何正確地做到這一點。上面有很多不必要的代碼來訪問字典,然後檢查結果,然後對數組/字典的字符串表示形式進行操作,後面幾乎肯定會是一個痛點,特別是如果您不花時間瞭解關於這段代碼的壞處,並最終將它們重新實現到處 – 2013-03-12 09:16:27

+0

我這樣做的原因是因爲沒有結果時返回的json在albummatches關鍵字下包含一個煩人的字符返回。我會發布json,在上面的問題中沒有返回結果。 – Cheese1223 2013-03-12 14:11:59

+0

你還可以在有專輯時提供一個簡短的片段嗎? – 2013-03-12 14:14:34

相關問題