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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:58:17  来源:igfitidea点击:

How to create an NSDictionary with multiple keys?

iosobjective-cjsonkeynsdictionary

提问by donparalias

I am not sure if what I am going to ask is actually an NSDictionarywith multiple keys but ok.

我不确定我要问的是否实际上是NSDictionary带有多个键的,但是可以。

What I want to do is create an NSDictionarywith keys and values for my data and then convert it to JSONformat. The JSONformat 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 NSDictionarieslike 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 NSDictionarybe , to fit the JSONformat?? Thanks for reading my post , and sorry if any confusion.

但是上面的格式并不是关于键值的。所以我的问题是如何NSDictionary, 以适应JSON格式?感谢您阅读我的帖子,如有任何混淆,请见谅。

回答by Rui Peres

You can have a NSDictionaryinside 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 NSMutableDictionarywith keys and objects:
->key eventDateobject NSString
->key eventLocationobject NSMutableDictionarywith keys and objects:
----> key latitudeobject NSNumber
----> key longitudeobject NSNumber
-> key textobject NSString
-> key imageDataobject NSStringlater converted to NSData
-> key imageFormatobject NSString
-> key expirationTimeobject NSNumber
typekey for object NSString
titlekey 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"
                                 }]
                                }

                             ]
                         };