-2

how i can create Dictionary that when i create jsondata with it, json looks like :

 "historyStep":[

{
"counter": "50",
"timestamp": "1461674383632"
}
 ]

I did this :

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

but it looks :

 historyStep =     {
    counter = 810;
    timestamp = "1464957395241.447998";
};
Evgenii
  • 422
  • 2
  • 14
  • NSMutableDictionary *jsonDictMain create an array instead of this dictionary –  Jun 03 '16 at 12:46
  • 2
    You are missing a level: NSDictionary (top level) with NSArray of NSDictionary in the top level key "historyStep". – Larme Jun 03 '16 at 12:48

3 Answers3

1

You are missing a level: NSDictionary (top level) with NSArray of NSDictionary in the top level key 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];
Larme
  • 24,190
  • 6
  • 51
  • 81
0

Your code should be like,

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

It's output is,

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

You just missed one array between

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
0
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];

Use this code it will work perfectly.

Raza.najam
  • 769
  • 6
  • 15