user5513630
user5513630

Reputation: 1699

save jsondata to plist by "key":"value" format

Here i have get data and convert it in to nsdictionary and use plist to store. I also put some NSLog to check whether my data have stored or not.but when i read my plist that i have store json .in my console its saying Null.

code is edited

So here is my code.Does any thing i am missing or any other way like "Key"-"Value" format to store my json data to plist.

Your help will move to next feature .Thanks in advance.

If any other method are available with "Key"/"value"- kindly let me know in code.Because i am new to ios & not well in code

This is my Json. Also please see here it having 4 category .In each category it having some id,data,date..

Log output:

plistPath : /Users/maicr/Library/Developer/CoreSimulator/Devices/144C1221-F589-415C-8855-3E5AA06459F8/data/Containers/Data/Application/6F405C7F-43F4-4910-B2CF-15083345F38C/Documents/SampleData.plist
2015-11-03 01:54:12.156 demo[10450:550543] Data successfully saved.
2015-11-03 01:54:12.157 demo[10450:550543] str: (null)

Upvotes: 1

Views: 652

Answers (2)

anders
anders

Reputation: 4218

I've tested this and it works. Also better use of the NSErrors that you are not using. It also doesn't rely on the plist being already created..

NSString *returnData = @"{\"count\":3,\"most\":{\"count\":0,\"files\":[]},\"deleted\":{\"count\":0,\"files\":[]},\"original\":{\"count\":4,\"files\":[{\"created_time\":1431939838,\"last_modified_time\":1431939859,\"id\":8293015,\"name\":\"doc1.pdf\"},{\"created_time\":1431939845,\"last_modified_time\":1431939869,\"id\":8293017,\"name\":\"doc2.pdf\"},{\"created_time\":1431939854,\"last_modified_time\":1431939886,\"id\":8293019,\"name\":\"doc3.pdf\"},{\"created_time\":1431939872,\"last_modified_time\":1431939898,\"id\":8293023,\"name\":\"doc4.pdf\"}]},\"extra\":{\"count\":0,\"files\":[]}}";
NSData *data = [returnData dataUsingEncoding:NSUTF8StringEncoding]; // Your data may start as NSData to begin with...

NSError *parseError = nil;
NSDictionary *jsonResults = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&parseError];

if(parseError != nil )
{
    NSLog(@"Parse Error: %@", parseError.userInfo);
}
else
{
    //Build plist path
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"SampleData.plist"];
    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSMutableDictionary *plistData = nil;
    if([fileManager fileExistsAtPath:path])
    {
        // Pull existing plist
        plistData = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
    }
    else
    {
        // Create new plist
        plistData = [NSMutableDictionary new];
    }

    [plistData setObject:jsonResults forKey:@"myJSONData"];
    [plistData writeToFile:path atomically:TRUE];


    // .....

    // Retrieve Data..
    NSMutableDictionary *savedJSONResults = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
    if([[savedJSONResults allKeys] containsObject:@"myJSONData"])
    {
        NSDictionary *myJSONData = [savedJSONResults objectForKey:@"myJSONData"];
        NSLog(@"%@", myJSONData);
    }

}

Pull JSON from web source using AFNetworking API

[AFHTTPRequestOperationManager manager] GET:@"www.someAPI.com/resource" parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
    NSDictionary *JSONKeyValuePairs = (NSDictionary *)responseObject; // serialized JSON data for you to do stuff with...

    // Do PLIST saving stuff here...

} failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
    NSAssert(false, @"I failed."); // do something better than asset for release build since they are not compiled in. EI handle failures gracefully somehow.
}

Upvotes: 1

vadian
vadian

Reputation: 285082

Since you wrote

convert it in to nsdictionary and use plist to store

and also the JSON confirms that (it starts with a curly brace), it can't be an array.

Instead

NSArray *stringsArray = [NSArray arrayWithContentsOfFile:plistPath];

write

NSDictionary *stringsDictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];

PS: I recommend to completely use the URL related API

Upvotes: 0

Related Questions