Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save NSArray or NSDictionary to JSON file ?

JSONKit provides awesome way to decode JSON file, what is the best way to do the opposite way to encode objects to JSON file ?
BTW: Here is tutorial for loading & saving with XML.

Thanks

like image 286
Forrest Avatar asked Dec 03 '22 07:12

Forrest


2 Answers

iOS 5 now has everything needed to do that :

NSError *error = nil;
NSOutputStream *outputStream = [NSOutputStream outputStreamToFileAtPath:filepath append:yesOrNo];
[outputStream open];

[NSJSONSerialization writeJSONObject:nsDicOrNSArrayObject 
                            toStream:outputStream 
                             options:0 
                               error:&error];
[outputStream close];
like image 175
Ben G Avatar answered Dec 09 '22 16:12

Ben G


JSONKit also has everything you need to retrieve a JSON value from NSArrays and NSDictionarys.

NSString *jsonString = [yourDictionary JSONString];
like image 27
Paul Tiarks Avatar answered Dec 09 '22 15:12

Paul Tiarks