让TouchJSON返回可变对象吗? [英] Have TouchJSON return mutable objects?

查看:115
本文介绍了让TouchJSON返回可变对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从网络服务收到一些json。我使用TouchJSON库解析它。
我保留数据以供用户更改某些值,然后我想将其返回给Web服务。

I am receiving some json from a web service. I parse this using the TouchJSON library. I keep the data around for the user to change certain values and then I want to return it to the web service.

我得到的JSON对象包含NSDictionary对象中的对象,如下所示:

The JSON object I get contains NSDictionary Objects within the object, like this:

[
    {
        "id": null,
        "created_at": 12332343,
        "object": {
            "name": "Some name",
            "age" : 30 
        },
        "scope": "it stuff",
        "kind_id": 1,
        "valid": true,
        "refering_statement": {
            "id": 95 
        },
        "user": {
            "id": 1 
        } 
    }
]

如果我想更改此字典中的值,我不能,因为TouchJSON返回的对象不可变。

If I want to change values in this dictionary, I can't because the returned objects from TouchJSON are not mutable.

有没有办法让TouchJSON返回可变对象?

或者有没有办法让Objective C生成一个NSDictionary及其所有子代都可变?

或者我是否必须通过每个NSDictionary NSDictionary并将所有数据复制到一个可变副本然后重新插入所有内容?

or do I have to just go through every NSDictionary in NSDictionary and copy all data into a mutable copy and then reinsert everything?

希望有人可以帮助我解决这个问题:)提前感谢。

Hope someone can help me out on this one:) thanks in advance.

推荐答案

TouchJson有一个选项可以返回可变对象而不是正常对象。 (我通过查看源代码找到它。)默认是返回它的副本,而不是mutablecopy。

TouchJson has an option to return mutable object rather than normal. (I found it by looking at source code.) Default is to return its "copy", not "mutablecopy".

NSError *error = nil;    
jsonString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF32BigEndianStringEncoding];
CJSONDeserializer *jsondeserializer = [CJSONDeserializer deserializer];
jsondeserializer.scanner.options = kJSONScannerOptions_MutableContainers;
NSMutableDictionary *jsonitems = [[NSMutableDictionary alloc] initWithDictionary:[jsondeserializer deserializeAsDictionary:jsonData error:&error]];

这篇关于让TouchJSON返回可变对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆