在Objective C中创建JSON格式 [英] creating JSON format in Objective C

查看:120
本文介绍了在Objective C中创建JSON格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我希望以下面提到的格式创建JSON的应用程序,

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

"Students" : {
    "results": {
        "Grade1": {
            "studentresult": "pass",
            "marksheet": "provided"
        },
        "ID": 01,
        "Name": "Student1", 
    }
}

我使用以下代码创建相同的,

I am using the following code to create the same,

NSMutableDictionary *gradedetails = [[NSMutableDictionary alloc] init];
[gradedetails setObject:@"pass" forKey:@"studentresult"];
[gradedetails setObject:@"provided" forKey:@"marksheet"];

NSMutableDictionary *sdetails = [[NSMutableDictionary alloc] init];
[sdetails setObject:@"01" forKey:@"ID"];
[sdetails setObject:@"Name" forKey:@"Student1"];

NSMutableDictionary *grade = [[NSMutableDictionary alloc] init];
[grade setObject:gradedetails forKey:@"Grade1"];

NSMutableArray *rarray = [[NSMutableArray alloc] init];
[rarray addObject:grade];
[rarray addObject:sdetails];

NSMutableDictionary *results = [[NSMutableDictionary alloc] init];
[results setObject:rarray forKey:@"results"];

NSMutableDictionary *stud = [[NSMutableDictionary alloc] init];
[stud setObject:rdic forKey:@"Students"];

NSData *jsondata = [NSJSONSerialization dataWithJSONObject:stud options:NSJSONWritingPrettyPrinted error:&error];

我收到以下格式,

"Students" : {
"results" : [
  {
    "Grade1" : {
      "studentresult" : "pass",
      "marksheet" : "provided" 
    }
  },
  {
    "ID" : "01",
    "Name" : "Student1"
  }
]

}
}

有人可以帮我创建格式。

could someone please help me in creating the format.

谢谢。

推荐答案

所需数据,您可以这样做。只需转换JSon中的stud dict或任何其他您想要的格式。删除该数组,你不需要它,正如你所提到的那样。

Required Data , You can get this way . Just convert that stud dict in JSon or any other format you want. Remove that array , You don't need it , As you mentioned it in the required format.

NSMutableDictionary *gradedetails = [[NSMutableDictionary alloc] init];
[gradedetails setObject:@"pass" forKey:@"studentresult"];
[gradedetails setObject:@"provided" forKey:@"marksheet"];

NSMutableDictionary *sdetails = [[NSMutableDictionary alloc] init];
[sdetails setObject:@"01" forKey:@"ID"];
[sdetails setObject:@"Name" forKey:@"Student1"];
[sdetails setObject:gradedetails forKey:@"Grade1"];

NSMutableDictionary *results = [[NSMutableDictionary alloc] init];
[results setObject:sdetails forKey:@"results"];

NSMutableDictionary *stud = [[NSMutableDictionary alloc] init];
[stud setObject:results forKey:@"Students"];

NSLog(@"Required Format Data is %@",stud);

这篇关于在Objective C中创建JSON格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆