I am not sure if what I am going to ask is actually an NSDictionary
with multiple keys but ok.
What I want to do is create an NSDictionary
with keys and values for my data and then convert it to JSON
format. The JSON
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
},
"type": "ELDIARIOMONTANES",
"title": "accIDENTE"
}
I ve only used NSDictionaries
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 NSDictionary
be , to fit the JSON
format??
Thanks for reading my post , and sorry if any confusion.
You know you can have a
NSDictionary
inside anotherNSDictonary
right?Now with Objective-C literals there is a much better, easier, and cleaner way of accomplishing this. Here is your exact dictionary with this new syntax:
if you want multiple categories , you can follow this format
How to Create NSArray and with Access for object using NSDictionary ?
... Create NSArray
...to Access to NSArray Object Using NSDictionary
Here is the structure:
Your root object is
NSMutableDictionary
eventData
- key for objectNSMutableDictionary
with keys and objects:->key
eventDate
objectNSString
->key
eventLocation
objectNSMutableDictionary
with keys and objects:----> key
latitude
objectNSNumber
----> key
longitude
objectNSNumber
-> key
text
objectNSString
-> key
imageData
objectNSString
later converted toNSData
-> key
imageFormat
objectNSString
-> key
expirationTime
objectNSNumber
type
key for objectNSString
title
key for objectNSString