2013-07-04 53 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