-3

I have NSMutableDictionary inside this values are present like this

{
    0 =     {
        imageangle = "0.09630692";
        imageheight = "129.0245";
        imagepath = "assets-library://asset/asset.PNG?id=BD849AC4-EDF2-4E05-B5F8-F5EE34385A97&ext=PNG";
        imagescalex = "85.48777";
        imagescaley = "85.48777";
        imagewidth = "129.0245";
        mainheight = 282;
        mainwidth = 316;
    };
    memes =     {
        alpha = 1;
        alphaFont = 1;
        blue = 1;
        blueFont = 0;
        green = 1;
        greenFont = "0.5";
        memestext = "Kishore kumar kumar ";
        red = 0;
        redFont = 1;
        textheight = 34;
        textscalex = 13;
        textscaley = "30.5";
        textwidth = 316;
    };
}

Using this i like to export this dictionary in .json file.

After that i am converting into JSONSTRING:

     NSError *error;
     NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dickeyjson                  options:NSJSONWritingPrettyPrinted                                                                                    error:&error];
NSString* aStr;
aStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

My Question:

Is it possible to export it as .json and store it to local storage?

Kishore Kumar
  • 4,265
  • 3
  • 26
  • 47

2 Answers2

0

You certainly can export a string to a .json file, and this would be an outline of a correctly error handling way to do so:

do {
    let jsonString = "{\"what\":\"ever\"}"
    let jsonFile = "file.json"
    guard let docs = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true).first else {
        throw NSError(domain: "what-no-Documents-folder??", code: 1, userInfo: nil)
    }
    let path = NSURL(fileURLWithPath: docs).URLByAppendingPathComponent(jsonFile)
    try text.writeToURL(path, atomically: false, encoding: NSUTF8StringEncoding)
} catch {
    fatalError("Writing failed!")
}

and to read it back in,

do {
    let path = NSURL(fileURLWithPath: "same as for writing")
    let jsonString = try NSString(contentsOfURL: path, encoding: NSUTF8StringEncoding)
}
catch {
    fatalError("Reading failed!")
}

However, it is probable that you want to put this JSON back into a dictionary when reading, yes? In that case, there's no need to convert to JSON, you should just use NSDictionary's writeToURL method directly, using the same outline as above; as I don't believe there's any JSON-representable object not conforming to NSCoder and therefore archivable with writeToFile/writeToURL.

Alex Curylo
  • 4,744
  • 1
  • 27
  • 37
0

What exactly is the problem? You convert the dictionary to NSData using NSJSONSerialization. Then you write the NSData to a file using a variant of writeToFile: or writeToURL: There is no reason to go through NSString, it's just a waste of time, memory, and battery life.

To read the data, use dataWithContentsOfFile or dataWithContentsOfURL, and again NSJSONSerialization. If you use a mapped file, the NSData doesn't even use memory; that would be useful if your data is multiple megabytes.

gnasher729
  • 51,477
  • 5
  • 75
  • 98