简体   繁体   English

JSONKit:更改和序列化JSON属性的值

[英]JSONKit: changing and serializing JSON attribute's value

I am using JSONKit to parse JSON string into NSDictionary: 我正在使用JSONKit将JSON字符串解析为NSDictionary:

NSDictionary *deserializedData = [jsonString objectFromJSONString];

My question is: how can I change the dictionary values and get a changed JSON String? 我的问题是:如何更改字典值并获取更改的JSON字符串?

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! 提前致谢!

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. mutableObjectFromJSONString方法将直接创建一个可变字典,从而节省时间和内存。

// 
// we begin with our string in json format
//
NSString *jsonString = [[NSString alloc] initWithString:@"{\"1\":\"Hole 1: Rossy Robinson - $25\",\"2\":\"Hole 7: Davey Ambrose - $25\",\"3\":\"Hole 14: Ross Robinson - $25\"}"];

//
// convert the json string to an NSMutableDictionary
//
NSError *e;
NSMutableDictionary *JSONdic = [NSJSONSerialization JSONObjectWithData: [jsonString dataUsingEncoding: NSUTF8StringEncoding] options: NSJSONReadingMutableContainers error: &e];

//
// change a value and add a new value in the dict
//
NSLog(@"before: object for key 1 is: %@", [JSONdic objectForKey:@"1"]);
[JSONdic setObject:@"xxx" forKey:@"1"];
[JSONdic setObject:@"Phil McQuitty" forKey:@"2"];

//
//convert dictionary object to json data
//
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:JSONdic options:NSJSONWritingPrettyPrinted error:&e];

//
// convert the json data back to a string
//
NSString *jsonText = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];\

//
// print out the final results
//
NSLog(@"back to string: %@", jsonText);

NSDictionary is an immutable dictionary, you need NSMutableDictionary to change the data. NSDictionary是一个不可变的字典,您需要NSMutableDictionary来更改数据。 I'm not sure about JSONKit, but the built-in Cocoa JSON parser has a flag to return the data in mutable containers. 我不确定JSONKit,但是内置的Cocoa JSON解析器有一个标志来返回可变容器中的数据。

In worst case, you can do something like that: 在最坏的情况下,您可以执行以下操作:

NSMutableDictionary* data = [NSMutableDictionary dictionaryWithDictionary:[jsonString objectFromJSONString]];
[data setObject:[NSNumber numberWithInt:iRatings] forKey:@"ratings"];

You try to change an immutableobject. 您尝试更改一个不可变的对象。

NSMutableDictionary *deserializedData = [NSMutableDictionary dictionaryWithDictionary: [jsonString objectFromJSONString]];

This is a mutable dictionary and you can change the values in it. 这是一个可变的字典,您可以更改其中的值。

You try like this: 您可以这样尝试:

NSMutableDictionary *deserializedData = [NSMutableDictionary dictionaryWithDictionary: [jsonString objectFromJSONString]];

and then change the values: 然后更改值:

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

For NSDictionary we cannot add or change values, thats why application is crashing. 对于NSDictionary我们无法添加或更改值,这就是应用程序崩溃的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM