J'ai besoin de stocker JSON
données JSON
analysées dans la list des propertylist
comme ci-dessous en utilisant l' objective C
Ma réponse JSON du server:
{ "response":{ "A":{ "name":"Arun", "age":"20", "city":"SFO", "subject":2 }, "B":{ "name":"Benny", "age":"20", "city":"SFO", "subject":1 }, "C":{ "name":"Nani", "age":"30", "city":"SFO", "subject":0 } }, "inprogressdata":{ }, "dataspeed":"112 milliseconds..." }
J'essaie comme ci-dessous la méthode de stockage dans propertylist
. Dans cet Item 0 , 1, 2
Sa réponse appelée JSON A, B, C
valeurs Into Array et Item devrait comme un dictionary.
NOTE :
Les valeurs A, B, C
augmenteront en fonction de la réponse JSON, je veux dire que ce n'est pas statique. Cela ira jusqu'à Z peut être.
Dans ce Array
, la première chose que chaque valeur doit stocker en tant que dictionary
. Dans ce dictionary, vous devez append des valeurs de strings comme ci-dessous et si le nombre de sujets est supérieur à 0 en fonction de ce nombre. Il doit créer un tableau de dictionary comme ci-dessous Image.
Par exemple :
-Root | |-----Objects [Array Value] | |------Item 0(A) [Dictionary Value] | |------name (Ssortingng) |------age (Ssortingng) |------city (Ssortingng) |------subject (Number) // If number 2 then need to create "Objects_Subjectcount" [Array Values] |------Objects_Subjectcount (Array) | |------Item 0 [Dictionary Value] |------Item 1 [Dictionary Value]
Essayez ce code
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error]; NSDictionary *response = JSON[@"response"]; NSArray *objects = [response allValues]; NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSSsortingng *documentsPath = paths.firstObject; NSSsortingng *plistPath = [documentsPath ssortingngByAppendingPathComponent:@"File.plist"]; NSError *writeError = nil; NSData *plistData = [NSPropertyListSerialization dataWithPropertyList:objects format:NSPropertyListXMLFormat_v1_0 options:NSPropertyListImmutable error:&writeError]; if(plistData){ [plistData writeToFile:plistPath atomically:YES]; } else { NSLog(@"Error in saveData: %@", error); }
Si ma compréhension de la deuxième partie de votre question est correcte, le code sera comme celui-ci
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers error:&error]; NSDictionary *response = JSON[@"response"]; NSArray *keys = [response allKeys]; NSMutableArray *objects = [NSMutableArray new]; for (NSSsortingng *key in keys) { NSMutableDictionary *object = response[key]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"subject = %@",object[@"subject"]]; NSArray *objectsWithSameSubject = [objects filteredArrayUsingPredicate:predicate]; NSInteger subjects = [object[@"subject"] integerValue]; if (subjects > 0) { NSMutableArray *Objects_Subjectcount = [NSMutableArray new]; [object setObject:Objects_Subjectcount forKey:@"Objects_Subjectcount"]; for (NSInteger i = 0; i < subjects; i++) { [Objects_Subjectcount addObject:object];// object or anything you need } } [objects addObject:object]; } NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSSsortingng *documentsPath = paths.firstObject; NSSsortingng *plistPath = [documentsPath ssortingngByAppendingPathComponent:@"File.plist"]; NSError *writeError = nil; NSDictionary *finalDict = @{@"Objects": objects}; NSData *plistData = [NSPropertyListSerialization dataWithPropertyList:finalDict format:NSPropertyListXMLFormat_v1_0 options:NSPropertyListImmutable error:&writeError]; if(plistData){ [plistData writeToFile:plistPath atomically:YES]; } else { NSLog(@"Error in saveData: %@", error); }
Les étapes sont,
1. Create JSON with mutable containers and leaves. 2. Iterate through all keys and values. 3. Check whether the subject value of object is greater than 0 in the existing subjects array? if yes add the object to Objects_Subjectcount. Add the object to objects array.(Don't forget to set an NSMutable array with key Objects_Subjectcount) to the object. 4 . Make final dictionary to be written as plist - wrap the objects array in a dictionary with key "objects" 5. Convert the dictionary to plist data and write it
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); // get documents path NSSsortingng *documentsPath = [paths objectAtIndex:0]; // get the path to our Data/plist file NSSsortingng *plistPath = [documentsPath ssortingngByAppendingPathComponent:@"Data.plist"]; // set the variables to the values in the text fields NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: @"pradip", @"25",@"Ahmedabad",@"Computer-Science", nil] forKeys:[NSArray arrayWithObjects: @"Name", @"AGE",@"CITY",@"Subject", nil]]; // create NSData from dictionary NSSsortingng *error = nil; NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error]; // check is plistData exists if(plistData) { // write plistData to our Data.plist file [plistData writeToFile:plistPath atomically:YES]; } else { NSLog(@"Error in saveData: %@", error); }