이번 미션에서는 어떠한 데이터를 받아서 json문자열을 생성 해야 한다.
지금 까지는 간단한 key : value 형태의 json 문자열을 생성하여 WAS통신 하는 앱을 개발 했는데 그것은 간단한 NSArray 의 key value를 SBJson 모듈에 넣어서 생성 하면 간단 했다.
detal은 mode에 따라서 여러번 반복된다.
마찬가지로 sleep도 마찬가지이다.
//sleep.detail
NSMutableDictionary * sleep_detail1 = [[NSMutableDictionary alloc] init];
[sleep_detail1 setValue:@"200" forKey:@"startTime"];
[sleep_detail1 setValue:@"200" forKey:@"stopTime"];
[sleep_detail1 setValue:@"200" forKey:@"settingstartTime"];
[sleep_detail1 setValue:@"200" forKey:@"settingstopTime"];
[sleep_detail1 setValue:@"Awake" forKey:@"mode"];
NSMutableDictionary * sleep_detail2 = [[NSMutableDictionary alloc] init];
[sleep_detail2 setValue:@"200" forKey:@"startTime"];
[sleep_detail2 setValue:@"200" forKey:@"stopTime"];
[sleep_detail2 setValue:@"200" forKey:@"settingstartTime"];
[sleep_detail2 setValue:@"200" forKey:@"settingstopTime"];
[sleep_detail2 setValue:@"Light Sleep" forKey:@"mode"];
NSMutableDictionary * sleep_detail3 = [[NSMutableDictionary alloc] init];
[sleep_detail3 setValue:@"200" forKey:@"startTime"];
[sleep_detail3 setValue:@"200" forKey:@"stopTime"];
[sleep_detail3 setValue:@"200" forKey:@"settingstartTime"];
[sleep_detail3 setValue:@"200" forKey:@"settingstopTime"];
[sleep_detail3 setValue:@"Deep Sleep" forKey:@"mode"];
NSArray *sleep_detail_array = [NSArray arrayWithObjects: sleep_detail1,
sleep_detail2,
sleep_detail3,
nil ];
//sleep
NSMutableDictionary * sleepData = [[NSMutableDictionary alloc] init];
[sleepData setValue:@"200" forKey:@"startTime"];
[sleepData setValue:@"200" forKey:@"stopTime"];
[sleepData setValue:sleep_detail_array forKey:@"detail"];
[sleepData setValue:@"200" forKey:@"calorie"];
//activity.detail
NSMutableDictionary * activity_detail1 = [[NSMutableDictionary alloc] init];
[activity_detail1 setValue:@"200" forKey:@"startTime"];
[activity_detail1 setValue:@"200" forKey:@"stopTime"];
[activity_detail1 setValue:@"200" forKey:@"stepCnt"];
[activity_detail1 setValue:@"200" forKey:@"distance"];
[activity_detail1 setValue:@"Non-move" forKey:@"mode"];
NSMutableDictionary * activity_detail2 = [[NSMutableDictionary alloc] init];
[activity_detail2 setValue:@"200" forKey:@"startTime"];
[activity_detail2 setValue:@"200" forKey:@"stopTime"];
[activity_detail2 setValue:@"200" forKey:@"stepCnt"];
[activity_detail2 setValue:@"200" forKey:@"distance"];
[activity_detail2 setValue:@"Walking" forKey:@"mode"];
NSMutableDictionary * activity_detail3 = [[NSMutableDictionary alloc] init];
[activity_detail3 setValue:@"200" forKey:@"startTime"];
[activity_detail3 setValue:@"200" forKey:@"stopTime"];
[activity_detail3 setValue:@"200" forKey:@"stepCnt"];
[activity_detail3 setValue:@"200" forKey:@"distance"];
[activity_detail3 setValue:@"Fast Walking" forKey:@"mode"];
NSMutableDictionary * activity_detail4 = [[NSMutableDictionary alloc] init];
[activity_detail4 setValue:@"200" forKey:@"startTime"];
[activity_detail4 setValue:@"200" forKey:@"stopTime"];
[activity_detail4 setValue:@"200" forKey:@"stepCnt"];
[activity_detail4 setValue:@"200" forKey:@"distance"];
[activity_detail4 setValue:@"Running" forKey:@"mode"];
NSArray *activity_detail_array = [NSArray arrayWithObjects: activity_detail1,
activity_detail2,
activity_detail3,
activity_detail4,
nil ];
//activity
NSMutableDictionary * activity = [[ NSMutableDictionary alloc] init];
[activity setValue:@"200" forKey:@"statTime"];
[activity setValue:@"200" forKey:@"stopTime"];
[activity setValue:activity_detail_array forKey:@"detail"];
[activity setValue:@"200" forKey:@"calorie"];
//sync
NSMutableDictionary * sync = [[ NSMutableDictionary alloc] init];
[sync setValue:@"SEOUL" forKey:@"timezone"];
[sync setValue:activity forKey:@"activity"];
[sync setValue:sleepData forKey:@"sleep"];
NSError *err;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:sync options:NSJSONWritingPrettyPrinted error:&err];
NSLog(@"JSON = %@", [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);