使用NSMutableArray获取对象?

时间:2012-10-17 07:20:32

标签: iphone xcode nsmutablearray

我有这样的JSON:

[{
"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"。我怎么能这样做? 请帮帮我!

5 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

objectAtIndex

获取valueForKeyNSMutableArray的价值
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"];

:)

答案 2 :(得分:0)

NSMutableArray *idArray = [[NSMutableArray alloc] init];

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

答案 3 :(得分:0)

让NSArray * info数组包含您的数据。在这里,我将检索您的数据并将其保存在NSMutableArray名称,密码和ID中。

在viewDidLoad方法中将这些数组初始化为

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

在重新审阅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];
   }

答案 4 :(得分:0)

示例,Array包含字符串对象。

Acccess:
NSString.FromHandle(deviceList.ValueAt(i));