开发者

How to save NSArray or NSDictionary to JSON file ?

JSONKit provides awesome way to decode JSON file, what开发者_JAVA百科 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 also has everything you need to retrieve a JSON value from NSArrays and NSDictionarys.

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

if you want to convert dictionary into json data then use dataWithJSONObject

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜