Pour une application, je souhaite créer un fichier JSON au format mentionné ci-dessous,
"Students" : { "results" : [ { "Grade1" : { "studentresult" : "pass", "marksheet" : "provided" } }, { "ID" : "01", "Name" : "Student1" } ]
6 Réponses :
Pour obtenir le format que vous souhaitez en dehors de celui-ci, vous devez emballer des informations dans ce format particulier.
Votre problème est juste à propos de: P>
NSMutableArray *rarray = [[NSMutableArray alloc] init]; [rarray addObject:grade]; [rarray addObject:sdetails];
Données requises, vous pouvez obtenir de cette façon. Il suffit de convertir ce stud dict dans JSON ou tout autre format souhaité. Supprimer ce tableau, vous n'en avez pas besoin, comme vous l'avez mentionné dans le format requis.
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);
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.
dépend de votre code:
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]; } }
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
Vous voulez obtenir au-dessus de la sortie au format fixe ?? et transmettre cette valeur JSON au serveur ??
Oui Paras, je veux obtenir le format correct et transmettre le format JSON.
C'est faux:
[SetObject de note: gradédetails ForeKey: @ "Grade1"]; code>