Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 创建NSDictionary_Ios_Objective C_Json_Nsdictionary - Fatal编程技术网

Ios 创建NSDictionary

Ios 创建NSDictionary,ios,objective-c,json,nsdictionary,Ios,Objective C,Json,Nsdictionary,如何创建字典,当我使用它创建jsondata时,json看起来像: "historyStep":[ { "counter": "50", "timestamp": "1461674383632" } ] 我这样做: NSMutableDictionary*jsonDictOth = [[NSMutableDictionary alloc]init]; [jsonDictOth setObject:@(810) forKey:@"counter"]; [jsonDictOth setObj

如何创建字典,当我使用它创建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*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 *innerDictionary = [[NSDictionary alloc]initWithObjectsAndKeys:@"50", @"counter",@"1461674383632", @"timestamp", nil];
NSArray *array = [[NSArray alloc]initWithObjects:innerDictionary, nil];
NSDictionary *outerDict = [[NSDictionary alloc]initWithObjectsAndKeys:array, @"historyStep", nil];

使用此代码,它将完美地工作。

您缺少一个级别:
NSDictionary
(顶级)在顶级键
historyStep
中使用
NSArray
NSDictionary

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

NSMutableDictionary*jsonDictMain创建数组而不是此字典您缺少一个级别:NSDictionary(顶级),NSDictionary的NSArray位于顶级键“historyStep”中。为什么这么详细?Objective-C文字有什么问题?