1

I have created JSON data store Into Plist. Now the problem is after JSON data storage, I need to add two set of keys into every array of dictionary items like below Image_2.The key name isParent - Boolean YES and isChild - Boolean YES with levels like mentioned below Image_2.

Now I have below structure of plsit datas Its perfectly working by below code.

enter image description here

I need to add two keys for outside of object subjectcount and inside of objectsubjectcount red marked datas.

enter image description here

NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers error:&error];
 NSDictionary *response = JSON[@"response"];
 NSArray *keys = [response allKeys];

 NSMutableArray *objects = [NSMutableArray new];
 for (NSString *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);
 NSString *documentsPath = paths.firstObject;
 NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"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);
 } 

NOTE : all the datas store by JSON but after storage need to add additional values by manually! Thats I am trying

Helpme
  • 59
  • 5
  • Thank you @Itteh Kitteh! Do you know how to make above process? – Helpme Dec 29 '15 at 04:56
  • inside the inner frolic do [Objects_Subjectcount addObject:@{@"level":@(0), @"isChild": @(YES)}]; And in outer loop [object setObject:@(yes) forKey:@"isParent"]; – Johnykutty Dec 29 '15 at 08:53
  • Try something before asking, you just copied the code from previous answer and asking help... – Johnykutty Dec 29 '15 at 08:54
  • Please go through the following links http://stackoverflow.com/help/how-to-ask http://stackoverflow.com/help/dont-ask – Johnykutty Dec 29 '15 at 09:04

2 Answers2

0

The object from response[key] is immutable so it can't be modified as below:

[object setObject:Objects_Subjectcount forKey:@"Objects_Subjectcount"];

Making it mutable as below, it can be modified by any method add/remove/set.

NSMutableDictionary *object = [response[key] mutableCopy];

Hope using below modified block, you would see required changes.

NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers error:&error];
NSDictionary *response = JSON[@"response"];
NSArray *keys = [response allKeys];

NSMutableArray *objects = [NSMutableArray new];
for (NSString *key in keys) {
    NSMutableDictionary *object = [response[key] mutableCopy];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"subject = %@",object[@"subject"]];
    NSArray *objectsWithSameSubject = [objects filteredArrayUsingPredicate:predicate];
    NSInteger subjects = [object[@"subject"] integerValue];
    if (subjects > 0) {

        [object setObject:@"" forKey:@"level"];
        [object setObject:@(YES) forKey:@"isParent"];

        NSMutableArray *Objects_Subjectcount = [NSMutableArray new];
        for (NSInteger i = 0; i < subjects; i++) {
            [Objects_Subjectcount addObject:@{@"level":@(0), @"isChild":@(YES)}];
        }
        [object setObject:Objects_Subjectcount forKey:@"Objects_Subjectcount"];

    }
    [objects addObject:object];
}

NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = paths.firstObject;
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"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);
}
bunty
  • 629
  • 5
  • 8
  • I need to add two manual values after Json data load above mentioned red mark values i need to add after json data stoarage.@bunty – Apple_Ajay Dec 29 '15 at 05:37
  • Could you please help me? I need to add two keys after loaded JSON data! – Apple_Ajay Dec 29 '15 at 05:48
  • I just changed my answer, hope you will get desired results. – bunty Dec 29 '15 at 05:53
  • @bunty sorry for distubing you If u modified within my code I can easily understand! – Helpme Dec 29 '15 at 05:55
  • I copied whole your code including my changes. – bunty Dec 29 '15 at 06:03
  • @bunty Its not working! I have checeked my plist there nothing new values added – Helpme Dec 29 '15 at 06:11
  • Can you share original plist, I will look into it? – bunty Dec 29 '15 at 06:14
  • @bunty please help me dude! – Helpme Dec 29 '15 at 06:36
  • @Helpme, please check now. As you have not given plist, I have created on my own named "file_name.plist". Rename it with yours and try. – bunty Dec 29 '15 at 06:43
  • @bunty Its not working! Please check and give me once again!If I use your code nothing data into plist! – Apple_Ajay Dec 29 '15 at 10:46
  • @Apple_Ajay, can you provide sample project in which there is that code and plist? – bunty Dec 29 '15 at 10:57
  • @bunty I Can you read my question again! – Helpme Dec 29 '15 at 11:03
  • Updated answer again. I got file as output that looks same on this link: http://www.image-share.com/upload/3131/110.jpg – bunty Dec 29 '15 at 11:40
  • @bunty thank you so much BTW isChild values actually Dictionary of Array Its automattically created by JSON into plist If I create NSMutableArray *Objects_Subjectcount = [NSMutableArray new]; then Its replacing JSON responses. – Helpme Dec 30 '15 at 04:39
  • for (NSInteger i = 0; i < subjects; i++) { [Objects_Subjectcount addObject:@{@"level":@(0), @"isChild":@(YES)}]; } [object setObject:Objects_Subjectcount forKey:@"Objects_Subjectcount"]; If I am using I can add ischild keys and values but I am losting my JSOn data. Need to add with JSOn data@bunty – Apple_Ajay Dec 30 '15 at 12:47
0
NSData *plistData = [NSPropertyListSerialization dataWithPropertyList:finalDict format:NSPropertyListXMLFormat_v1_0 options:NSPropertyListMutable error:&writeError];

Retrieve data from plist like this.

NSMutableDictionary *plistDic = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];

Now suppose you want to update first array of dictionary then

NSMutableArray *array = [[NSMutableArray alloc]init:[plistDic objectAtIndex:0]]

You can do for loop if you want to change all array data...Right i am just using first object data..

NSMutableDictionary *dict = [[NSMutableDictionary alloc]init:[array objectAtIncex:0]];
[dict addObject:"yourdata" forKey:"yourkey"];
[array replaceObject:dict atIndex:0];
[plistDic replaceObjectAtIndex:0 withObject:array];

And last

[plistDic writeToFile:plistPath atomically:YES];
Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
Bhoomi Jagani
  • 2,413
  • 18
  • 24
  • Could you please update my code above. I need to add two keys on under Items and another one boolean key need to add subjectcount Items for all Items need to add. Check above posted Image! – Helpme Dec 29 '15 at 05:54
  • Can u update up answer! – Helpme Dec 29 '15 at 06:13