IOS:如何从字典中获取单个键的值

时间:2016-02-05 10:31:42

标签: ios json parsing

我有一个字典,其形式是我想获得“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];

2 个答案:

答案 0 :(得分:1)

尝试以下代码..

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

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

答案 1 :(得分:0)

尝试使用此代码,因为它是嵌套的NSDictionary。

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