0

I have created an NSDictionary named "myData" which contains the following JSON response:

{
listInfo =     (              
{ 
    date = 1392157366000;       
    dateAsString = "02/11/2014 22:22:46";     
    id = 6;  
    address = 542160e0000c;     
    myLevel = 13;  
},            
{

    date = 1392155568000;        
    dateAsString = "02/11/2014 21:52:48";      
    id = 5;    
    address = 542160e0000c;     
    myLevel = 13;

}
    );
}

I need to retrieve each of the [dateAsString] key/value pairs. I've tried: NSString *dateAsString=[[myData valueForKeyPath:@"dateAsString"][0] objectForKey:@"myData"]; without any luck.

Any suggestions are greatly appreciated.

  • Go to json.org and learn the JSON syntax. It only takes 5-10 minutes. Then ***look*** at what you have. You erroneously omitted the leading `{` in the dump indicating the start of a dictionary. The only element in the dictionary is "listInfo". The value of that element is an NSArray (as indicated by the `()` brackets), and that array contains 2 dictionaries (bracketed by `{}`). If you take it one step at a time it's incredibly easy to take that apart and extract the data. – Hot Licks May 15 '14 at 23:50

1 Answers1

2

I think this will work:

NSArray* dateStringArray = [listInfo valueForKeyPath:@"@unionOfObjects.dateAsString"]

I believe it will give you an array of strings. If you need to stuff that back in a dictionary, that should be fairly easy.

It's not clear what your "myData" looks like... so I used the listInfo array of dicts shown.

stevesliva
  • 5,351
  • 1
  • 16
  • 39