0

I have recently started Application development on MAC OS 10.6, I am trying to modify a "key/value" pair in a local JSON file on my MAC machine using SBJSON. I have successfully read the value of a key, but I am not able to get that how to modify the value of a key and synchronize this to the JSON file. Lets suppose, I have a following JSON Data int o a local file:

{
    "name": {
            "fName":"John",
            "lName":"Doe"
            }
            }

And i want to change the value of "fName" to something else, like Robert.

I have tried alot searching about it, but got no clue... Can anyone help me.

I am using SBJSON Framework!

Code:

NSString *filePath = @"/Users/dev/Desktop/SQLiteFile/myJSON2.json";

NSData   *myData    = [NSData dataWithContentsOfFile:filePath];

NSString *responseString = [[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding];
NSLog(@"FILE CONTENT : %@", responseString);

SBJsonParser *jsonParser = [[SBJsonParser alloc] init];

NSDictionary * dictionary = (NSDictionary*)[jsonParser objectWithString:responseString error:NULL];



[dictionary setObject:@"Robert" forKey:@"fName"];

//
// Code for writing this change into the file, which i needed.
//

[jsonParser release];

Sebass van Boxel
  • 2,514
  • 1
  • 22
  • 37
  • Could you modify your question to show a bit of code that demonstrates how you converted the JSON data to a NSDictionary (or whatever type you used to extract the "`fName`" value)? If you can do that, I (or somebody else) may be able to more easily help you to come up with a solution. – Michael Dautermann Oct 03 '12 at 07:39
  • Hi Michael !!! I have added code. In which i am getting the JSON data and converting it into a dictionary, then modifying the dictionary. I want these changes to be affected in my local JSON file. – Aditya Singh Oct 03 '12 at 08:41

1 Answers1

0

You want a mutable deep copy of your dictionary. Then you'll be able to modify it.

Cyrille
  • 25,014
  • 12
  • 67
  • 90
  • add some code to show how to do that and I'll upvote this potentially fine answer. – Michael Dautermann Oct 03 '12 at 08:46
  • This has already been answered a thousand times on Stack Overflow. – Cyrille Oct 03 '12 at 08:47
  • Is there any other way i can modify a value corresponding to a key in my JSON file. – Aditya Singh Oct 03 '12 at 08:55
  • OOOOkay. You're right about that. For the benefit of Aditya, [here is one question](http://stackoverflow.com/questions/9159738/writing-json-using-sbjson) and [here is another](http://stackoverflow.com/questions/9308674/jsonkit-changing-and-serializing-json-attributes-value) (although this latter one is using a different JSON serializer... same concept, tho!). – Michael Dautermann Oct 03 '12 at 08:56
  • Aditya, you cannot modify something that's not mutable, by definition. – Cyrille Oct 03 '12 at 08:59
  • Ok Cyrille, So you want to say that instead of NSDictionary i should take NSMutableDictionary to make the deep mutable copy of my data. And make change sinto that dictionary. But one question how to commit those change sinto the file ??? – Aditya Singh Oct 03 '12 at 09:12
  • Can you tell me an example code how to get the json data into a mutable copy of dictionary !!! and how to commit these changes into file ??? – Aditya Singh Oct 03 '12 at 09:35
  • Search SO for "mutable deep copy of nsdictionary". Then, to write a NSMutableDictionary to a JSON file, you use the same methods than to write a NSDictionary (the former descends from the latter). I don't know how to do it with SBJson, but with JSONKit it's super simple. – Cyrille Oct 03 '12 at 10:08