如何创建具有多个密钥的NSDictionary? [英] How to create an NSDictionary with multiple keys?
问题描述
我不确定我要问的是否实际上是具有多个键但可以的NSDictionary
.
I am not sure if what I am going to ask is actually an NSDictionary
with multiple keys but ok.
我想做的是用数据的键和值创建一个NSDictionary
,然后将其转换为JSON
格式. JSON
格式看起来完全像这样:
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"
}
我只这样使用NSDictionaries
:
NSArray *keys = [NSArray arrayWithObjects:@"eventDate", @"eventLocation", @"latitude" nil];
NSArray *objects = [NSArray arrayWithObjects:@"object1", @"object2", @"object3", nil];
dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
但是以上格式并不完全是关于键-值的.
所以我的问题是NSDictionary
是什么,以适应JSON
格式?
感谢您阅读我的文章,并感到困惑.
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.
推荐答案
您可以在另一个NSDictonary
中包含一个NSDictionary
:
You can have a NSDictionary
inside another NSDictonary
:
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"];
这篇关于如何创建具有多个密钥的NSDictionary?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!