繁体   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