【问题标题】:IOS: how to get the value of a single key from dictionaryIOS:如何从字典中获取单个键的值
【发布时间】:2016-02-05 10:31:41
【问题描述】:

我有一本字典,形式为我想获取“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];

【问题讨论】:

  • 请提供更多信息...您想要所有 id 还是特定 id?
  • 是的,我只想要字典形式的 id
  • 你能完整回复一下吗....
  • 它包含在一个包含 5940 个元素的数组中,因此无法显示完整的响应

标签: ios json parsing


【解决方案1】:

试试下面的代码..

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

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

【讨论】:

  • 检查“dict6”是否为空
  • 它给出了结果,但我没有找到确切的结果
  • 这就是为什么我要求提供更多信息..你想获取什么 id..什么是标准..
【解决方案2】:

试试这个代码,因为它是嵌套的 NSDictionary。

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

【讨论】:

  • 它给出空输出
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-02
相关资源
最近更新 更多