简体   繁体   中英

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

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];

JSONKit还具有从NSArrays和NSDictionarys检索JSON值所需的一切。

NSString *jsonString = [yourDictionary JSONString];

Have you tried SBJson ? I've used it for both parsing and generation of JSON in several projects.

NSData *json1= [ NSJSONSerialization dataWithJSONObject :info options:NSJSONWritingPrettyPrinted error:&error];

如果要将字典转换为json数据,请使用dataWithJSONObject

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM