ios 获取 nsdictionary 中特定键的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16233425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:21:32  来源:igfitidea点击:

get values of particular key in nsdictionary

iphoneiosobjective-cnsdictionary

提问by Minkle Garg

I saved the jsonparsing result in a dictionarywhich look like:

我将json解析结果保存为dictionary如下所示:

{
 "statusCode":"200",
"body":[
  {
     "status":"success",
     "remarks":null
  }
],

"data":[
     "abcd":[
        {
           "category":"a",
           "title":"b",
           "id":"24"
        },
        {
           "category":"c",
           "title":"crd",
           "id":"65"
        },
        {
           "category":"ds",
           "title":"sd",
           "id":"18"
        }

     ]
  },
  {
     "efgh":[
        {
           "category":"ds",
           "title":"sd",
           "id":"18"
        },
        {
           "category":"sd",
           "title":"sd",
           "id":"1"
        }

                             ]
  },
  {
     "ijkl":[
        {
           "category":"ds",
           "title":"sd",
           "id":"18"
        },
        {
           "category":"sd",
           "title":"sd",
           "id":"1"
        }

     ]
  }
 ]
}

The data for key @"data" can be saved into an array by using

键@"data" 的数据可以通过使用保存到一个数组中

NSMutableArray *getdata=[[NSMutableArray alloc]init];
getcat=[results objectForKey:@"data"];

Now what should I do for the values(category, title, id)inside the first index i.e. "abcd".

现在我应该为values(category, title, id)第一个索引内部做些什么,即"abcd".

If anyone has any knowledge please look upon that.

如果有人有任何知识,请看一下。

Thanks to all.

谢谢大家。

回答by Anoop Vaidya

Following will give you the desired object

以下将为您提供所需的对象

NSDictionary *dict=[results valueForKeyPath:@"data.abcd"][0];

For individual:

对于个人:

NSString *categoryString=[[results valueForKeyPath:@"data.abcd"][0] objectForKey:@"Category"];
NSString *idString=[[results valueForKeyPath:@"data.abcd"][0] objectForKey:@"id"];
NSString *titleString=[[results valueForKeyPath:@"data.abcd"][0] objectForKey:@"title"];

Also,

还,

NSString *categoryString=dict[@"Category"];
NSString *idString=dict[@"id"];
NSString *titleString=dict[@"title"];

回答by Balu

Check like this:

像这样检查:

NSString *value=[[[getcate valueForKey:@"abcd"] objectAtIndex:0] valueForKey:@"category"];

回答by borrrden

That is an array so use objectAtIndex:(i.e. [getcat objectAtIndex:0]or getcat[0])

这是一个数组,所以使用objectAtIndex:(ie [getcat objectAtIndex:0]or getcat[0])