6

对于我想要创建格式如下所述的 JSON 的应用程序,

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

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

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"
  }
]

} }

有人可以帮我创建格式。

谢谢。

4

6 回答 6

3

所需数据,您可以通过这种方式获得。只需将该螺柱字典转换为 JSON 或您想要的任何其他格式。删除该数组,您不需要它,正如您在所需格式中提到的那样。

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);
于 2013-04-17T10:18:24.507 回答
3

取决于您的代码:

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

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

NSMutableDictionary *stud = [[NSMutableDictionary alloc] init];
[stud setObject:results forKey:@"Students"];
于 2013-04-17T10:27:18.337 回答
2
NSDictionary *gradedetails = @{@"studentresult" : @"pass", @"marksheet" : @"provided"};
NSDictionary *grade = @{ @"Grade1" : gradedetails}
NSDictionary *sdetails = @{@"ID" : @"01", @"Student1" : @"Name"};
NSArray *resultsArray = @[grade, sdetails];
NSDictionary *results= @{@"results" : resultsArray};
NSDictionary *stud = @{@"Students" : results};

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

我想知道为什么很少有开发人员使用这种表示法

于 2013-04-17T13:30:05.160 回答
1
    check this code-
  NSMutableDictionary *students=[NSJSONSerialization JSONObjectWithData:webData        options:0 error:nil];
      for (NSDictionary *dictionofstudents in students)
      {
           NSMutableDictionary *results=[dictionofstudents objectForKey:@"results"];
           for (NSDictionary *dictionofresults in results)
           {
               NSMutableDictionary *grade1=[dictionofresults objectForKey:@"Grade1"];
               for (NSDictionary *dictionofgrade1 in grade1) 
               {
                   NSString *studentresult=[dictionofgrade1 objectForKey:@"studentresult"];
                   NSString *marksheet=[dictionofgrade1 objectForKey:@"marksheet"];
                   [arrayofstudentresult addObject:studentresult];
                   [arrayofmarksheet addObject:marksheet];
               }
               NSString *ID=[dictionofresults objectForKey:@"ID"];
               NSString *name=[dictionofresults objectForKey:@"Name"];
              [arrayofID addObject:ID];
              [arrayofname addObject:name];
           }
      }
于 2013-04-17T12:27:08.027 回答
0

为了获得您想要的格式,您必须以该特定格式打包信息。

你的问题就在这里:

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

所需的格式没有数组,那么为什么要创建数组?

给你一个提示:

您应该正好创建 4 个字典,并且没有数组。

于 2013-04-17T10:17:00.613 回答
0
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);

它可能会帮助你。

于 2013-04-17T10:22:01.710 回答