2013-07-04 54 views
0

我是初学者在ios和我的一个活动:这是我的nsstring,现在我想从这个字符串获取“LocationId”,但我有问题.....我尝试在数组中添加此字符串,之后得到LocationId但我也有错误....如何从ios中获取Nsstring的值?

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"]]; 

OR

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....... 
+0

请出示您的真实代码。前两行永远不会编译。 –

回答

0

您提供的代码将无法编译,但我猜,你有一些JSON作为NSString

NSError *e; 
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] 
                options:NSJSONReadingMutableContainers 
                 error: &e]; 

if (!json) 
    NSLog(@"Error: %@",e); 
else 
    NSString *locationID = [json objectForKey:@"LocationId"]; 
+0

注意:检查错误条件的正确方法是检查*返回值*,在这种情况下,if(json == nil)'。 –

+0

不检查错误参数,在检查错误参数之前检查返回值是否为零。请参阅http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ErrorHandlingCocoa/CreateCustomizeNSError/CreateCustomizeNSError.html#//apple_ref/doc/uid/TP40001806-CH204-BAJIIGCC,然后查看信息框清单2-1 – Abizern

+0

感谢您的提示。无论如何:没有理由downvote这个答案.. – tilo

0

当然你NSUnknownKeyException。 NSString没有LocationIdaccessor

如果您想解析JSON,请使用JSON解析器,例如NSJSONSerialization

哦,并且当您的意思是objectForKey:时不要使用valueForKey:

3

做这样

你必须使用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); 
+0

真的非常感谢您的答案实际上,我已经解析然后得到这个字符串后,我已经应用了乌尔代码.... – rahul

+1

如果你好我的答案接受它,这样它会帮助其他 – Venkat

2

您提供的代码有很多问题,我怀疑你已经复制并粘贴正确的。例如Nsstring将不会编译。

但是,一般而言,您已经创建了类似JSON字典的字符串,但语法不正确。并且您试图获取未在NSString上定义的属性的值,这是导致错误的原因。

您正在寻找这样的事情:

NSDictionary *dictionary = @{ @"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 = dictionary[@"LocationId"]; 
+0

Op不使用_dictionary_方法。 'valueForKey:'是键值方法,可以与任何具有NSKeyValueCoding的类一起使用。 – Kreiri

+0

当然。固定。谢谢。 – Abizern