2013-08-03 65 views
0

我想解析以下與我的JSON解析器。我嘗試了幾種方法,但實際的JSON序列化NSJSONSerialization總是拋出錯誤(實際上它拋出NSArray * termArray = [dataDictionary objectForKey:@「term」];但原因是,因爲dataDictionary沒有正確的響應所以我徘徊,爲什麼,因爲URL請求似乎工作JSON解析結果發送無法識別的選擇器

'NSInvalidArgumentException' 的,原因是:「 - [__ NSCFArray objectForKey:]:無法識別的選擇發送到實例

[ 
{ 
    "id": 20765985, 
    "url": "http://quizlet.com/20765985/basic-portuguese-flash-cards/", 
    "title": "Basic Portuguese", 
    "created_by": "aUser", 
    "term_count": 4, 
    "created_date": 1362858462, 
    "modified_date": 1362858462, 
    "has_images": false, 
    "subjects": [ 
     "Portuguese" 
    ], 
    "visibility": "only_me", 
    "editable": "only_me", 
    "has_access": true, 
    "description": "", 
    "has_discussion": true, 
    "lang_terms": "pt", 
    "lang_definitions": "en", 
    "creator": { 
     "username": "aUser", 
     "account_type": "free", 
     "profile_image": "https://quizlet.com/a/i/animals/38.ndeQ.jpg", 
     "id": 6602337 
    }, 
    "terms": [ 
     { 
      "id": 696519541, 
      "term": "mesa", 
      "definition": "table", 
      "image": null 
     }, 
     { 
      "id": 696519542, 
      "term": "amigo", 
      "definition": "friend", 
      "image": null 
     }, 
     { 
      "id": 696519543, 
      "term": "leite", 
      "definition": "milk", 
      "image": null 
     }, 
     { 
      "id": 696519544, 
      "term": "vender", 
      "definition": "sale", 
      "image": null 
     } 
    ], 
    "display_timestamp": "In March 2013" 
} 

]

這是我使用解析

NSURL *myURL = [NSURL URLWithString:@"https://api.quizlet.com/2.0/users/<myUser>/sets?access_token=<myAccessToken>&whitespace=1"]; 

NSData *jsonData = [NSData dataWithContentsOfURL:myURL]; 

NSError *error = nil; 

NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error]; 
NSArray *termArray = [dataDictionary objectForKey:@"term"]; 

回答

0

在你的JSON開始的代碼你有[,這意味着JSON是一個數組。所以你不能以字典的形式訪問它。這就是爲什麼日誌說它是一個數組,你會得到異常。

相關問題