6

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];

I am getting in the following format,

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

} }

could someone please help me in creating the format.

Thanks.

3
  • you want to get above fixed format output right?? and pass this json value to server?? Commented Apr 17, 2013 at 10:11
  • Yes Paras, i want to get the format correct and pass the json format. Commented Apr 17, 2013 at 10:13
  • This is wrong: [grade setObject:gradedetails forKey:@"Grade1"]; Commented Apr 17, 2013 at 10:54

6 Answers 6

3

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);
Sign up to request clarification or add additional context in comments.

Comments

3

Depends on your code:

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

Comments

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];

I wonder why few developper use this notation

Comments

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

Comments

0

In order to get the format you want out of it, you have to pack information in that particular format.

Your problem is right about here:

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

The desired format has no arrays, so why are you creating an array?

A hint for you:

You should be creating exactly 4 dictionaries, and no arrays.

Comments

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);

It may helps you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.