iphone - 如何从 ios 中的 Nsstring 获取值?

标签 iphone ios objective-c

我是 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/

相关文章:

ios - 为什么 SKShapeNode 会撕裂?

ios - 如何在选择的 UITableViewCell 行中传递值

ios - Sprite 套件 CGPoint 未按预期工作

ios - 我什么时候必须给 Magical Record 一个上下文?

ios - CollectionView 在 StackView 中消失(Swift)

php - 使用 PHP 向 IOS 设备发送通知

iphone - 使用存档对象提交应用程序商店

iphone - 为什么我的 iPhone 应用程序在后台继续运行?

iphone - 从服务器获取数据的问题

iphone - NSData 附加数据 : UIImageJPEGRepresentation() returns nil