8

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

我想要做的是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"
}

我只用过NSDictionaries这样的:

NSArray *keys = [NSArray arrayWithObjects:@"eventDate", @"eventLocation", @"latitude"  nil];
NSArray *objects = [NSArray arrayWithObjects:@"object1", @"object2", @"object3", nil]; 
dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

但是上面的格式不全是key-value。所以我的问题是如何NSDictionary适应JSON格式?感谢您阅读我的帖子,如有任何困惑,请见谅。

4

5 回答 5

37

你可以有一个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"];
于 2012-06-26T11:15:52.137 回答
17

现在有了 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"]);
于 2014-09-15T14:08:53.800 回答
3

如何使用 NSDictionary 创建 NSArray 并访问对象?

... 创建 NSArray

NSArray *studentkeys = [NSArray arrayWithObjects:@"studentName", @"studentBirthDate", @"studentCity", @"studentMobile"  nil];

NSArray *objects = [NSArray arrayWithObjects:@"Pravin", @"27/08/1990", @"Bhavnagar",@"7878007531", nil]; 

...使用 NSDictionary 访问 NSArray 对象

NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];                                  
于 2015-09-02T10:20:17.957 回答
1

这是结构:
您的根对象是NSMutableDictionary
eventData-NSMutableDictionary具有键和对象的对象的键:
->键eventDate对象NSString
->具有键和eventLocation对象NSMutableDictionary的键对象:
---->键latitude对象NSNumber
---->键longitude对象NSNumber
->键text对象NSString
- > key imageDataobjectNSString后来转换为NSData
-> key imageFormatobject NSString
-> key expirationTimeobject NSNumber
typekey for object NSString
titlekey for objectNSString

于 2012-06-26T11:13:36.800 回答
0

如果你想要多个类别,你可以遵循这个格式

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

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

                             ]
                         };
于 2017-02-28T04:03:31.660 回答