TouchJSON 返回可变对象吗?

发布于 2024-10-21 05:06:47 字数 772 浏览 1 评论 0原文

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

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

[
    {
        "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 返回的对象不可变。

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

或者有没有办法让 Objective C 使 NSDictionary 及其所有子项可变?

或者我必须这样做遍历 NSDictionary 中的每个 NSDictionary 并将所有数据复制到可变副本中,然后重新插入所有内容?

希望有人能帮助我解决这个问题:) 提前致谢。

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.

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 
        } 
    }
]

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

Is there a way to have have TouchJSON return mutable objects?

or is there a way to have Objective C make an NSDictionary and all its children mutable?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦在深巷 2024-10-28 05:06:47

TouchJson 可以选择返回可变对象而不是普通对象。 (我通过查看源代码找到了它。)默认值是返回其“副本”,而不是“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 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]];
肥爪爪 2024-10-28 05:06:47

您可以从这样的字典创建可变字典。

(假设你的 json 解析字典被命名为 jsonDictionary)

NSMutableDictionary *userDictionary = [NSMutableDictionary dictionaryWithDictionary:jsonDictionary];

希望能为你解决这个问题。

You can create a mutable dictionary from a dictionary like this.

(assuming your json parsed dictionary was named jsonDictionary)

NSMutableDictionary *userDictionary = [NSMutableDictionary dictionaryWithDictionary:jsonDictionary];

Hope that solves it for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文