don*_*ias 7 json key objective-c nsdictionary ios
我不确定我要问的是实际上是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"
}
Run Code Online (Sandbox Code Playgroud)
我只用过NSDictionaries这样的:
NSArray *keys = [NSArray arrayWithObjects:@"eventDate", @"eventLocation", @"latitude" nil];
NSArray *objects = [NSArray arrayWithObjects:@"object1", @"object2", @"object3", nil];
dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
Run Code Online (Sandbox Code Playgroud)
但上述格式并非全部都与关键价值有关.所以我的问题是如何NSDictionary适应JSON格式?感谢您阅读我的帖子,对不起,如果有任何困惑.
Rui*_*res 36
你知道你可以拥有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"];
Run Code Online (Sandbox Code Playgroud)
Fir*_*iro 15
现在有了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"]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
62649 次 |
| 最近记录: |