Can I write JSON data to a file in iOS/Objective-C?

ios, objective-c

Solution

There is a class specifically made for this, its called NSJSONSerialization.

You read it like this:

NSArray* jsonResponse = [NSJSONSerialization JSONObjectWithData:theResponse
                                                        options:kNilOptions
                                                          error:&error];

and write it like this:

NSData* jsonData = [NSJSONSerialization dataWithJSONObject:userDetails
                                                   options:kNilOptions 
                                                     error:&error];

Problem

I need to write JSON data to a file using Objective-C. My data looks something like this: ``` data = { "NrObjects" : "7", "NrScenes" : "5", "Scenes" : [ { "dataType" : "label", "position" : [20, 20, 300, 300], "value" : "Hello" }, { "dataType" : "label", "position" : [20, 60, 300, 300], "value" : "Hi There" } ] } ``` It may get more complex than this, but what I need to know is whether I can do this with Obj-C, i.e., create an object of this form, write the data to a file, and read it back.

Original source