creating JSON format in Objective C

后端 未结 6 459
感动是毒
感动是毒 2020-12-10 07:19

For an application i want to create a JSON in format as mentioned below,

\"Students\" : {
    \"results\": {
        \"Grade1\": {
            \"studentresul         


        
6条回答
  •  南笙
    南笙 (楼主)
    2020-12-10 08:00

    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    NSMutableDictionary *result = [[NSMutableDictionary alloc] init];
    NSMutableDictionary *grade = [[NSMutableDictionary alloc] init];
    [grade setValue:@"pass" forKey:@"studentresult"];
    [grade setValue:@"provided" forKey:@"marksheet"];
    [result setValue:grade forKey:@"Grade1"];
    [result setValue:@"01" forKey:@"ID"];
    [result setValue:@"Student1" forKey:@"Name"];
    [dict setValue:result forKey:@"results"];
    
    NSMutableDictionary *stdnt = [[NSMutableDictionary alloc] init];
    [stdnt setValue:dict forKey:@"Students"];
    NSError *error = nil;
    NSData *jsondata = [NSJSONSerialization dataWithJSONObject:stdnt options:NSJSONWritingPrettyPrinted error:&error];
    
    NSString *s = [[NSString alloc] initWithData:jsondata encoding:NSUTF8StringEncoding];
    NSLog(@"%@",s);
    

    It may helps you.

提交回复
热议问题