简体   繁体   中英

How to get value from Nsstring in ios?

I am beginner in ios and in one of my activity : this is my nsstring and now I want to get "LocationId" from this string but I have problem .....I try to add this string in array and after that get LocationId but that I have also error ....

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);

but I have error ....

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

Do like this

You have to use JSON Parser...

 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);

The code you have provided has many problems and I doubt that you have copied and pasted it correctly. eg Nsstring will not compile.

However, in general terms, you've created a string from something like a JSON dictionary, but the syntax is incorrect. And you are trying to get the value of a property that is not defined on NSString, which is the cause of your error.

You're looking for something like this:

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

The code you provided won't compile, but I guess that you got some JSON as a 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"];

Of course you get NSUnknownKeyException. NSString does not have LocationId accessor .

If you want to parse JSON, use JSON parsers, for example, NSJSONSerialization .

Oh, and don't use valueForKey: when you mean objectForKey: .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM