iphoneobjective-cjsonjsonkit

JSONKit: changing and serializing JSON attribute's value


I am using JSONKit to parse JSON string into NSDictionary:

NSDictionary *deserializedData = [jsonString objectFromJSONString];

My question is: how can I change the dictionary values and get a changed JSON String?

I've tried to change the dictionary values:

[deserializedData setObject:[NSNumber numberWithInt:iRatings] forKey:@"ratings"];   

But the app crashes in that line. What am I doing wrong?

Thanks in advance!


Solution

  • While the other answers are correct, what you really want in this case is:

    NSMutableDictionary *deserializedData = [jsonString mutableObjectFromJSONString];
    

    The mutableObjectFromJSONString method will create a mutable dictionary directly, which saves time and memory.