0

I have list of dictionaries in c#. How to create array of items for specific key from dictionary? In objective-C there is valueForKeyPath: method.

Here is an example of the list with dictionaries:

 [
   { "id": 1546, "description": “test_1” }, 
   { "id": 2228, "description": “test_2” }, 
   { "id": 2762, "description": "test_3” }
 ]

and I would like to get:

 [ 1546, 2228, 2762 ] 

for key id.

Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
Patryk Grabowski
  • 111
  • 1
  • 2
  • 5
  • Can you show an example? So sample data, what you've tried and the expected result. – Tim Schmelter Apr 24 '15 at 07:18
  • Yes of corse, here is my list with dictionaries: [ { "id": 1546, "description": “test_1” }, { "id": 2228, "description": “test_2” }, { "id": 2762, "description": "test_3” } and I would like to get: [ 1546, 2228, 2762 ] for key "id" – Patryk Grabowski Apr 24 '15 at 07:34
  • Edit your question instead and add the code nicely formatted. What is the expected result? – Tim Schmelter Apr 24 '15 at 07:36
  • Also, what type do they have? Why don't you use classes with meaningful properties instead of those abstruse dictionaries? – Tim Schmelter Apr 24 '15 at 07:39
  • I. e. because he gets a JSON object. BTW: `-valueForKeyPath:` works for custom classes as well. So you do not have to care, which class the instance object providing the data has. This is useful i. e. for UI programming. – Amin Negm-Awad Apr 24 '15 at 07:47
  • I am very new to c#. I will explain you what try to do. I try to use Json.NET to put response to my sqlite database. So each dictionary in previous example is JObject. I want to query my data base with list of id to return all records with id contained it this list. But I do not know hot to nicely create this List. I suppose it should be one line of code:) – Patryk Grabowski Apr 24 '15 at 07:58
  • Maybe this helps a bit: http://stackoverflow.com/questions/14129421/get-property-of-generic-class – Amin Negm-Awad Apr 24 '15 at 08:06

2 Answers2

0

Why don't you run a loop ex:

var list = new List<int>();
foreach(var dic in DictionaryList){
  if(dic.ContainsKey("id"))
    list.Add( dic["id"] );
}

First for loop is iterating through your list and grabbing each dictionary, the second for is graving each key value pair and you can then compare it to whatever is that you looking for. May be easier to do in a lambda expression, I just didn't have the time to write one up.

I think this is closer to what you were asking for. If I understand correctly you have a list of dictionaries and you trying to get a specific value out of each dictionary in the list. "ContainsKey" returns a bool, if its true then you can access that specific keyValue in the dictionary and add the value to a list or do whatever you want with it.

JEuvin
  • 866
  • 1
  • 12
  • 31
0

You can use this code:

NSMutableArray *arr;

for(int i=0;i<[givenArr count];i++){

NSDictionary *dic=givenArr[i];
[arr addObject:[dic valueForKey:@"id"]];
}
NSLog(@"%@",arr);
Saurabh Jain
  • 1,688
  • 14
  • 28