创建 NSDictionary

Posted

技术标签:

【中文标题】创建 NSDictionary【英文标题】:Creating NSDictionary 【发布时间】:2016-06-03 12:43:12 【问题描述】:

我如何创建字典,当我用它创建 jsondata 时,json 看起来像:

 "historyStep":[


"counter": "50",
"timestamp": "1461674383632"

 ]

我这样做了:

NSMutableDictionary*jsonDictOth = [[NSMutableDictionary alloc]init];
[jsonDictOth setObject:@(810) forKey:@"counter"];
[jsonDictOth setObject:@"1464957395241.447998" forKey:@"timestamp"];
NSMutableDictionary *jsonDictMain = [[NSMutableDictionary alloc]initWithObjectsAndKeys:jsonDictOth,@"historyStep", nil];
NSError*error;
NSData *data = [NSJSONSerialization dataWithJSONObject:jsonDictMain
                                                   options:NSJSONWritingPrettyPrinted
                                                     error:&error];

但看起来:

 historyStep =     
    counter = 810;
    timestamp = "1464957395241.447998";
;

【问题讨论】:

NSMutableDictionary *jsonDictMain 创建一个数组而不是这个字典 您缺少一个级别:NSDictionary(***),在***键“historyStep”中带有 NSDictionary 的 NSArray。 【参考方案1】:

您缺少一个级别:NSDictionary(***)与 NSArrayNSDictionary 在***键 historyStep

NSMutableDictionary *topLevel = [[NSMutableDictionary alloc] init];


NSArray *historySteps = [[NSMutableArray alloc] init];
//Here you may have a for loop in case there are more steps
NSDictionary *aStep = @@"counter":@"50", @"timestamp":@"1461674383632";
[historySteps addObject:aStep]

[topLevel setObject:historySteps forKey@"historyStep"];

NSError*error;
NSData *data = [NSJSONSerialization dataWithJSONObject:topLevel
                                               options:NSJSONWritingPrettyPrinted
                                                 error:&error];

【讨论】:

【参考方案2】:
NSDictionary *innerDictionary = [[NSDictionary alloc]initWithObjectsAndKeys:@"50", @"counter",@"1461674383632", @"timestamp", nil];
NSArray *array = [[NSArray alloc]initWithObjects:innerDictionary, nil];
NSDictionary *outerDict = [[NSDictionary alloc]initWithObjectsAndKeys:array, @"historyStep", nil];

使用此代码将完美运行。

【讨论】:

为什么这么冗长? Objective-C 文字有什么问题?【参考方案3】:

你的代码应该是这样的,

   NSMutableDictionary*jsonDictOth = [[NSMutableDictionary alloc]init];
[jsonDictOth setObject:@(810) forKey:@"counter"];
[jsonDictOth setObject:@"1464957395241.447998" forKey:@"timestamp"];

NSMutableArray *arr = [[NSMutableArray alloc]init];

[arr addObject:jsonDictOth];

NSMutableDictionary *jsonDictMain = [[NSMutableDictionary alloc]initWithObjectsAndKeys:arr,@"historyStep", nil];
NSLog(@"jsonMain is %@",jsonDictMain);
NSError*error;
NSData *data = [NSJSONSerialization dataWithJSONObject:jsonDictMain
                                               options:0
                                                 error:&error];

它的输出是,

jsonMain is 
historyStep =     (
            
        counter = 810;
        timestamp = "1464957395241.447998";
    
 );

你只是错过了一个数组

【讨论】:

以上是关于创建 NSDictionary的主要内容,如果未能解决你的问题,请参考以下文章

要用MFC创建一个窗口,如何创建?

oracle 安装完了,怎么创建用户和数据库???

oracle 安装完了,怎么创建用户和数据库???

mysql在创建表的时候可以创建字段那么创建新数据库的时候可以顺便创建表吗

Pod 的创建

桌面快捷方式如何创建 如何创建桌面快捷方式