2012-12-26 49 views
2

我有AFNetworking一個問題,從具有以下格式的API獲得JSON信息:AFNetworking JSON陣列而不值

{ 
    "result": [ 
     [ 
      { 
       "user": "test user", 
       "password": "test password", 
       "company": "test company" 
      } 
     ] 
    ] 
} 

通知{[[{}]]}(雙方括號中)。 但只有一個值「結果」。

我得到的錯誤:

ApiTest[83166:c07] -[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x7136a30 
ApiTest[83166:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x7136a30' 
*** First throw call stack: 
(0x1da6012 0x123de7e 0x1e314bd 0x1d95bbc 0x1d9594e 0x3d60 0x3122 0x16909 0x179953f 0x17ab014 0x179b7d5 0x1d4caf5 0x1d4bf44 0x1d4be1b 0x21397e3 0x2139668 0x18565c 0x2a9d 0x29c5) 
libc++abi.dylib: terminate called throwing an exception 

當我安裝在本地的文件,只是使用單組方括號「{[{}]}」的JSON數據由我AFNetworking/JSON處理粉項目代碼。

{ 
    "results": [ 
     { 
      "user": "test user", 
      "password": "test password", 
      "company": "test company" 
     } 
    ] 
} 

所以沒有雙括號套我可以成功地得到了「結果」使用:

self.results = [jsonObject objectForKey:@"result"]; 

如果我的項目是JavaScript的,我會使用這樣的:

var userId = data.result[0][0].user; 

不任何人都知道如何正確使用目標C選擇我的數組? 我花了幾天的調查,但我卡住了。

在此先感謝。

+0

請表明解析JSON數據,然後嘗試訪問分析的數據的代碼。 – Codo

回答

2

的問題是,你得到另一個數組裏的數組,所以你需要做的

NSArray *array = [jsonObject objectForKey:@"result"]; 
if (array.count > 0) 
    self.results = [array objectAtIndex:0]; 
+0

它的工作原理!謝謝伊斯梅爾,非常好。 – leuf