donparalias donparalias - 1 year ago 96
JSON Question

How to create an NSDictionary with multiple keys?

I am not sure if what I am going to ask is actually an

with multiple keys but ok.

What I want to do is create an
with keys and values for my data and then convert it to
format. The
format would look exactly like this :

"eventData": {
"eventDate": "Jun 13, 2012 12:00:00 AM",
"eventLocation": {
"latitude": 43.93838383,
"longitude": -3.46
"text": "hjhj",
"imageData": "raw data",
"imageFormat": "JPEG",
"expirationTime": 1339538400000
"title": "accIDENTE"

I ve only used
like this :

NSArray *keys = [NSArray arrayWithObjects:@"eventDate", @"eventLocation", @"latitude" nil];
NSArray *objects = [NSArray arrayWithObjects:@"object1", @"object2", @"object3", nil];
dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

But the above format is not all about key - value.
So my question is how would the
be , to fit the
Thanks for reading my post , and sorry if any confusion.

Answer Source

You know you can have a NSDictionary inside another NSDictonary right?

NSDictionary *eventLocation = [NSDictionary dictionaryWithObjectsAndKeys:@"43.93838383",@"latitude",@"-3.46",@"latitude", nil];

NSMutableDictionary *eventData = [NSDictionary dictionaryWithObjectsAndKeys:eventLocation,@"eventLocation", nil];
[eventData setObject:@"Jun 13, 2012 12:00:00 AM" forKey:@"eventDate"];
[eventData setObject:@"hjhj" forKey:@"text"];
NSMutableDictionary *finalDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:eventData,@"eventData", nil];
[finalDictionary setObject:@"ELDIARIOMONTANES" forKey:@"type"];
[finalDictionary setObject:@"accIDENTE" forKey:@"title"];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download