2014-06-24 28 views
1

我被解析爲NSDictionary後,在我的Swift代碼中收到以下JSON。在我的函數中,我試圖將「結果」塊中的JSON對象解析爲NSDIctionary [],但是這會引發運行時錯誤。我不明白爲什麼,因爲這是幾天前的工作。從JSON結果數據轉換爲NSDictionary []拋出EXC_BAD_INSTRUCTION運行時錯誤

{ 
"results": [ 
    { 
     "id": "3", 
     "name": "The National", 
     "slug": "thenational", 
     "facebook_url": "https://www.facebook.com/thenationalofficial/", 
     "twitter_url": "https://twitter.com/The_National", 
     "profile_image": "http://example.staging.com/media/profile_image/thumbnail_263x263/1352756032.jpg", 
     "_type": "artist", 
     "resource_uris": { 
     } 
    }, 
    { 
     "id": "5", 
     "name": "Mayer Hawthorne", 
     "slug": "mayerhawthorne", 
     "facebook_url": "https://www.facebook.com/MayerHawthorne", 
     "twitter_url": "https://twitter.com/MayerHawthorne", 
     "profile_image": "http://example.example.com/media/profile_image/thumbnail_263x263/1352755133.png", 
     "_type": "artist", 
     "resource_uris": { 
     } 
    }, 
    { 
     "id": "20", 
     "name": "I Play Maracas", 
     "slug": "iplaymaracas", 
     "facebook_url": "", 
     "twitter_url": "", 
     "profile_image": "http://staging.wedemand.com/images/en/img-list-home.gif", 
     "_type": "artist", 
     "resource_uris": { 
      "_demanded_by": null, 
      "demand_url": "http://ec2-54-86-17-163.compute-1.amazonaws.com/artists/20/?demand=1&access_token={}", 
      "dismiss_url": "http://ec2-54-86-17-163.compute-1.amazonaws.com/artists/20/?demand=0&access_token={}" 
     } 
    }, 
    { 
     "id": "35", 
     "name": "Black SuperHeros", 
     "slug": "blacksuperheros", 
     "facebook_url": "", 
     "twitter_url": "", 
     "profile_image": "http://staging.example.com/images/en/img-list-home.gif", 
     "_type": "artist", 
     "resource_uris": { 
     } 
    }, 
    { 
     "id": "49", 
     "name": "Ayman Elgadi", 
     "slug": "aymanelgadi", 
     "facebook_url": "", 
     "twitter_url": "", 
     "profile_image": "http://staging.example.com/images/en/img-list-home.gif", 
     "_type": "artist", 
     "resource_uris": { 
     } 
    }, 
    { 
     "id": "8874", 
     "name": "Lauri", 
     "slug": "lauri", 
     "facebook_url": "http://www.facebook.com/hughlaurieblues", 
     "twitter_url": "http://twitter.com/hughlaurieblues", 
     "profile_image": "http://staging.example.com/media/profile_image/thumbnail_263x263/lauri_profilepic.jpg", 
     "_type": "artist", 
     "resource_uris": { 
     } 
    } 
] 
} 

我IOS-SWIFT代碼接收的NSDictionary對象通過AFNetworking LIB被解析之後,並傳遞到鑄結果陣列的NSDictionary [],現投擲運行時錯誤的功能,而以前,它被加工。

(operation: AFHTTPRequestOperation!, responseObject: AnyObject!) in println("JSON: " + responseObject.description) 

var jsonResult: NSDictionary = responseObject as NSDictionary 

此jsonResult被傳遞到下面它試圖鑄造成的NSDictionary []

let allResults: NSDictionary[] = results["results"] as NSDictionary[] 

UPDATE功能: 我印刷類的結果,因爲它是被返回作爲__NSCFDictionary對象。

這裏What is an NSCFDictionary?是一個討論關於這個,並說這就像使用NSDictionary,但在我的情況下,它不工作。

+0

請提供一個代碼,你如何用swift解析這個json –

+1

在轉換前試着打印出它的類。 – Kevin

+0

嘗試打印'jsonResult'之前,它是否有'結果'鍵 –

回答

3

results的值不是JSON中的字典,而是數組。你應該用這樣的東西來得到它;

let allResults: NSArray = results["results"] as NSArray 
+0

我已經嘗試將其作爲NSArray進行投射,但也會引發運行時錯誤。 – srinivas

+0

什麼?它不是數組的數組,是嗎? – Chuck

+2

如果有的話,它將是一個單獨的'NSArray',而不是它們的字典。 @srinivas嘗試沒有'[]'。 – Kevin