Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 227
IIS7 Rewrite Avatar asked Nov 28 '12 03:11

IIS7 Rewrite


1 Answers

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];
like image 126
Pochi Avatar answered Sep 28 '22 05:09

Pochi