如何使用多个密钥创建NSDictionary?

时间:2021-12-16 23:10:54

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格式?感谢阅读我的帖子,对不起,如果有任何困惑。

5 个解决方案

#1


34  

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

你知道你可以在另一个NSDictonary中拥有一个NSDictionary吗?

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"];

#2


14  

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"]);

#3


2  

How to Create NSArray and with Access for object using NSDictionary ?

如何使用NSDictionary创建NSArray和Access对象?

... 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];                                  

#4


1  

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的键,带有键和对象: - > key eventDate对象NSString - > key eventLocation对象带有键和对象的NSMutableDictionary:----> key latitude object NSNumber --- - >关键经度对象NSNumber - >关键文本对象NSString - >关键imageData对象NSString以后转换为NSData - >关键imageFormat对象NSString - >关键expirationTime对象NSNumber类型键对象NSString标题键对象NSString

#5


0  

if you want multiple categories , you can follow this format

如果您想要多个类别,可以使用此格式

NSDictionary *jsonObject = @{
                           @"data1":@[
                              @{
                                @"title":@"A"

                                @"subData" : @[
                                @{
                                  @"title":@"aa"
                                 }]
                                }

                             ]
                         };

#1


34  

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

你知道你可以在另一个NSDictonary中拥有一个NSDictionary吗?

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"];

#2


14  

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"]);

#3


2  

How to Create NSArray and with Access for object using NSDictionary ?

如何使用NSDictionary创建NSArray和Access对象?

... 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];                                  

#4


1  

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的键,带有键和对象: - > key eventDate对象NSString - > key eventLocation对象带有键和对象的NSMutableDictionary:----> key latitude object NSNumber --- - >关键经度对象NSNumber - >关键文本对象NSString - >关键imageData对象NSString以后转换为NSData - >关键imageFormat对象NSString - >关键expirationTime对象NSNumber类型键对象NSString标题键对象NSString

#5


0  

if you want multiple categories , you can follow this format

如果您想要多个类别,可以使用此格式

NSDictionary *jsonObject = @{
                           @"data1":@[
                              @{
                                @"title":@"A"

                                @"subData" : @[
                                @{
                                  @"title":@"aa"
                                 }]
                                }

                             ]
                         };