簡體   English   中英

iOS:如何從字典中獲取單個鍵的值

[英]IOS: how to get the value of a single key from dictionary

我有一個字典,該字典的形式是我想獲取“ id”的值

{
  "procedures": [5950]
   0:  {
   "Procedures": {
                  "id": "1"
                  "procedure_name": "3d render w/o postprocess"
                  "procedure_code": "76376"
                 }
       }
  1:  {
        "Procedures": {
        "id": "2"
        "procedure_name": "3d rendering w/postprocess"
        "procedure_code": "76377"
 }

我想要獲取“ id”的值的所有方式

我把下面的代碼

NSString *post = @"api_key=............";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http:........"]];

[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

NSDictionary *dict6 = [self cleanJsonToObject:responseData];

嘗試以下代碼。

NSArray *ids = [[dict6 objectForKey:@"procedures"] valueForKeyPath:@"Procedures.id"];

要么

NSArray *ids = [[dict6 objectForKey:@"procedures"] valueForPath:@"id"];

請嘗試將此代碼嵌套NSDictionary。

[[[dict6 objectForKey:@"0"] objectForKey:@"Procedures"] objectForKey:@"id"];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM