TouchJSON 返回可变对象吗?
我从网络服务接收一些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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".
您可以从这样的字典创建可变字典。
(假设你的 json 解析字典被命名为 jsonDictionary)
希望能为你解决这个问题。
You can create a mutable dictionary from a dictionary like this.
(assuming your json parsed dictionary was named jsonDictionary)
Hope that solves it for you.