我是 ios 的初学者,在我的一项事件中:这是我的 nsstring,现在我想从这个字符串中获取“LocationId”,但我有问题......我尝试在数组中添加这个字符串,然后得到 LocationId 但我也有错误....
Nsstring *Str={"UserName":"ankitdemo","UserId":"08f11980-9920-4127-8fe7-78e1c40f6002","RoleName":"Doctor","RoleId":"87b4193c-1252-4505-a81b-2b49b8db57f3","FirstName":"Ankit","MiddleName":"","LastName":"Gupta","LocationId":"5f15648f-12ef-4534-a145-4044bc7c742e"}
Nsstring *LocationId=[NSString stringWithFormat:@"%@",[Str valueForKey:@"LocationId"]];
或
NSMutableArray *location =[[NSMutableArray alloc]init];
[location addObject:self.WebService.ptr];
NSLog(@"location id is %@",location);
LocationId=[NSString stringWithFormat:@"%@",[[location objectAtIndex:0]valueForKey:@"LocationId"]];
NSLog(@"location id is %@",LocationId);
但是我有错误....
ERROR
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0x881d6d0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key LocationId.'
Solve this problem.......
最佳答案
这样做
你必须使用 JSON 解析器...
NSDictionary *location = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]
options:NSJSONReadingMutableContainers
error: &e];
NSLog(@"location id is %@",location);
LocationId=[NSString stringWithFormat:@"%@",[[location objectAtIndex:0]valueForKey:@"LocationId"]];
NSLog(@"location id is %@",LocationId);
关于iphone - 如何从 ios 中的 Nsstring 获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17465085/