这里我得到我的远程服务器响应(JSON)以在这种方法中转换为NSDictionary。
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog (@"%@",dictionary);
我的输出文字如下:
输出:
{
"city" = "";
"state" = "";
"zip" = "";
"address" = 0;
"id" = 1;
"name" = "test name";
"country_name" = 0;
email = 0;
"fax" = 0;
"mobile" = 0;
phone = 1234567890;
website = "";
}
如果我曾经从NSDictionary获取名称变量。我得到了这个预期
-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0xcada210
-(void)addAccountDatails:(NSDictionary *)accArray{
NSLog(@"- %@",[accArray objectForKey:@"name"]);
}
如何在NSDictionary中获取名称值
答案 0 :(得分:1)
将NSDictionary
更改为NSArray
:
NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
[self addAccountDatails:array];
-(void)addAccountDatails:(NSArray *)accArray {
NSLog(@"- %@",[(NSDictionary*)accArray[0] objectForKey:@"name"]);
}
答案 1 :(得分:0)
请试试这个
NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
for(NSDictionary *dict in array)
{
NSLog(@"Name = %@", [dict objectForKey:@"name"]);
}