xcode 使用 NSMutableArray 获取对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12929119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using NSMutableArray get object?
提问by binhnt
I have JSON like this:
我有这样的 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"
}]
I want to use NSMutableArray
for get "id"
. How I can do it?
Help me, please!
我想NSMutableArray
用于 get "id"
。我该怎么做?请帮帮我!
采纳答案by Himanshu Mahajan
let NSArray *info array contains your data. Here I am going to retrive your data and save it in NSMutableArray name, password and id.
让 NSArray *info 数组包含您的数据。在这里,我将检索您的数据并将其保存在 NSMutableArray 名称、密码和 ID 中。
Initailise these arrays at viewDidLoad methods as
在 viewDidLoad 方法中初始化这些数组为
NSMutableArray name=[[NSMutableArray alloc] init];
NSMutableArray password=[[NSMutableArray alloc] init];
NSMutableArray id=[[NSMutableArray alloc] init];
Use following code after retriving JSON response
检索 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];
}
回答by Maulik
NSLog(@"%@",[[yourArray objectAtIndex:index] valueForKey:@"id"]);
回答by Paras Joshi
Get Value with objectAtIndex
and valueForKey
from NSMutableArray
获得值与objectAtIndex
和 valueForKey
从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"];
:)
:)
回答by Prasad G
NSMutableArray *idArray = [[NSMutableArray alloc] init];
for (NSDictionary *obj in yourJsonArray) {
[idArray addObject:[obj objectForKey:@"id"]];
}
NSLog(@"%@",idArray);
回答by Ali BOZO?
Example, Array have string objects.
例如,Array 有字符串对象。
Acccess:
NSString.FromHandle(deviceList.ValueAt(i));