ios 如何创建具有多个键的 NSDictionary?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11205974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create an NSDictionary with multiple keys?
提问by donparalias
I am not sure if what I am going to ask is actually an NSDictionary
with multiple keys but ok.
我不确定我要问的是否实际上是NSDictionary
带有多个键的,但是可以。
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 :
我想要做的是NSDictionary
为我的数据创建一个键和值,然后将其转换为JSON
格式。该JSON
格式将看起来就像这样:
{
"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 :
我只用过NSDictionaries
这样的:
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.
但是上面的格式并不是关于键值的。所以我的问题是如何NSDictionary
, 以适应JSON
格式?感谢您阅读我的帖子,如有任何混淆,请见谅。
回答by Rui Peres
You can have a NSDictionary
inside another NSDictonary
:
你可以有一个NSDictionary
里面另一个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"];
回答by Firo
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:
现在有了 Objective-C 文字,有一种更好、更简单、更清晰的方式来实现这一点。这是您使用这种新语法的确切字典:
NSDictionary *dictionary = @{
@"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"
};
// Prints: "43.93838383"
NSLog(@"%@", dictionary[@"eventData"][@"eventLocation"][@"latitude"]);
回答by Mr. Pravin Kantariya
How to Create NSArray and with Access for object using NSDictionary ?
如何使用 NSDictionary 为对象创建 NSArray 和访问权限?
... Create NSArray
... 创建 NSArray
NSArray *studentkeys = [NSArray arrayWithObjects:@"studentName", @"studentBirthDate", @"studentCity", @"studentMobile" nil];
NSArray *objects = [NSArray arrayWithObjects:@"Pravin", @"27/08/1990", @"Bhavnagar",@"7878007531", nil];
...to Access to NSArray Object Using NSDictionary
...使用 NSDictionary 访问 NSArray 对象
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
回答by graver
Here is the structure:
Your root object is NSMutableDictionary
eventData
- key for object NSMutableDictionary
with keys and objects:
->key eventDate
object NSString
->key eventLocation
object NSMutableDictionary
with keys and objects:
----> key latitude
object NSNumber
----> key longitude
object NSNumber
-> key text
object NSString
-> key imageData
object NSString
later converted to NSData
-> key imageFormat
object NSString
-> key expirationTime
object NSNumber
type
key for object NSString
title
key for object NSString
下面是结构:
你的根对象是NSMutableDictionary
eventData
-对于对象键NSMutableDictionary
连键和对象:
- >键eventDate
对象NSString
- >键eventLocation
对象NSMutableDictionary
连键和对象:
---->键latitude
对象NSNumber
---->键longitude
对象NSNumber
- >键text
对象NSString
- > 键imageData
对象NSString
后来转换为NSData
-> 键imageFormat
对象NSString
-> 键expirationTime
对象NSNumber
type
键为对象NSString
title
键为对象NSString
回答by Oshitha Wimalasuriya
if you want multiple categories , you can follow this format
如果你想要多个类别,你可以按照这个格式
NSDictionary *jsonObject = @{
@"data1":@[
@{
@"title":@"A"
@"subData" : @[
@{
@"title":@"aa"
}]
}
]
};