2012-10-17 93 views
1

我有JSON是這樣的:使用NSMutableArray獲取對象?

[{ 
"username":"bitu","password":"123456", "id":"1" 
}, 
{ 
"username":"admin","password":"admin", "id":"2" 
}, 
{ 
"username":"demo","password":"demo", "id":"3" 
}, 
{ 
"username":"admin","password":"admin", "id":"8" 
}, 
{ 
"username":"admin","password":"admin", "id":"5" 
}] 

我想用NSMutableArray爲獲得"id"。我如何做到這一點? 請幫幫我!從NSMutableArray

NSString *UserName = [[yourArray objectAtIndex:index] valueForKey:@"username"];//if you required 
NSString *Password = [[yourArray objectAtIndex:index] valueForKey:@"password"];//if you required 
NSString *UID = [[yourArray objectAtIndex:index] valueForKey:@"id"]; 

:)

+0

http://stackoverflow.com/questions/12927880/how-to-implement-searching-of-a-string-in-an-array-of-dictionaries/12929788#12929788 – Rajneesh071

+0

跟隨我的答案。 ..使用謂詞...並使用SBJSON類來解析json響應 – Rajneesh071

回答

0

objectAtIndexvalueForKey

1
NSLog(@"%@",[[yourArray objectAtIndex:index] valueForKey:@"id"]); 
1

獲得價值讓的NSArray *信息數組包含您的數據。在這裏,我將檢索您的數據並將其保存在NSMutableArray名稱,密碼和ID中。

Initailise這些陣列在viewDidLoad中方法如

NSMutableArray name=[[NSMutableArray alloc] init]; 
    NSMutableArray password=[[NSMutableArray alloc] init]; 
    NSMutableArray id=[[NSMutableArray alloc] init]; 

使用retriving JSON響應後下面的代碼

NSArray *info; // this array contains your data 
    int counter=[info count]; 

    for(int i=0;i<counter;i++) 
    { 
     NSDictionary *currentObject=[info objectAtIndex:i]; 
     NSString *userName=[currentObject objectForKey:@"username"]; 
     NSString *userPassword=[currentObject objectForKey:@"password"]; 
     NSString *userID=[currentObject objectForKey:@"id"]; 
     [name addObject:userName]; 
     [password addObject:userPassword]; 
     [id addObject: userID]; 
    } 
0
NSMutableArray *idArray = [[NSMutableArray alloc] init]; 

    for (NSDictionary *obj in yourJsonArray) { 
     [idArray addObject:[obj objectForKey:@"id"]]; 
    } 
NSLog(@"%@",idArray); 
0

實施例,陣列有字符串對象。

Acccess: 
NSString.FromHandle(deviceList.ValueAt(i)); 
相關問題