지금 까지는 간단한 key : value 형태의 json 문자열을 생성하여 WAS통신 하는 앱을 개발 했는데 그것은 간단한 NSArray 의 key value를 SBJson 모듈에 넣어서 생성 하면 간단 했다.
이번의 경우는
timezone
activity
-- startTime
-- stopTime
-- calorie
-- detail
--startTime
--stopTime
--stepCnt
-- distance
-- mode
sleep
-- startTime
-- stopTime
-- calorie
-- detail
-- startTime
-- stopTime
-- settingstartTime
-- settingstopTime
mode
위와 같은 json 형태가 되는데 active 의 하위 key가 있고 그중 detail은 또 하위를 가지며
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]);
실제 디버깅 정보로 위에서 생성한 데이터가 json 문자열로 어떻게 생성되었는지 확인해보면
다음과 같다.
JSON = {
"timezone" : "SEOUL",
"activity" : {
"stopTime" : "200",
"detail" : [
{
"startTime" : "200",
"stopTime" : "200",
"stepCnt" : "200",
"distance" : "200",
"mode" : "Non-move"
},
{
"startTime" : "200",
"stopTime" : "200",
"stepCnt" : "200",
"distance" : "200",
"mode" : "Walking"
},
{
"startTime" : "200",
"stopTime" : "200",
"stepCnt" : "200",
"distance" : "200",
"mode" : "Fast Walking"
},
{
"startTime" : "200",
"stopTime" : "200",
"stepCnt" : "200",
"distance" : "200",
"mode" : "Running"
}
],
"statTime" : "200",
"calorie" : "200"
},
"sleep" : {
"startTime" : "200",
"stopTime" : "200",
"detail" : [
{
"startTime" : "200",
"stopTime" : "200",
"settingstopTime" : "200",
"settingstartTime" : "200",
"mode" : "Awake"
},
{
"startTime" : "200",
"stopTime" : "200",
"settingstopTime" : "200",
"settingstartTime" : "200",
"mode" : "Light Sleep"
},
{
"startTime" : "200",
"stopTime" : "200",
"settingstopTime" : "200",
"settingstartTime" : "200",
"mode" : "Deep Sleep"
}
],
"calorie" : "200"
}
}
댓글 없음:
댓글 쓰기