将 Objective-C 对象序列化和反序列化为 JSON
我需要将 Objective-C 对象序列化和反序列化为 JSON 以存储在 CouchDB 中。人们是否有通用解决方案最佳实践的示例代码?我查看了一些 JSON 框架,它们都停留在 NSDictionary/NSArray 级别。即很多框架会将 NSDictionary/NSArray 序列化和反序列化为 JSON。但我仍然需要完成将 NSDictionary 转换为 Objective-C 对象的工作。
为了使事情变得更复杂,我的对象 A 可以引用对象 B 的 NSArray/NSDictionary。
我的问题与这个问题非常相似,只是增加了收集要求。
I need to serialize and deserialize objective-c objects into JSON to store in CouchDB. Do people have any example code for best practice for a general solution? I looked at a few JSON framework and they are stopped at the NSDictionary/NSArray level. i.e. A lot of framework will serialize and deserialize NSDictionary/NSArray into JSON. But I still have to do the work to convert NSDictionary into Objective-C objects.
To make things more complex, my Object A can have reference to an NSArray/NSDictionary of Object Bs.
My question is very similar to this question with addition of the collection requirement.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
最后我们可以使用 JSONModel 轻松解决这个问题。这是迄今为止最好的方法。 JSONModel 是一个通常基于类序列化/反序列化对象的库。您甚至可以使用基于非 nsobject 的属性,例如
int
、short
和float
。它还可以满足嵌套复杂的 JSON 的需求。考虑这个 JSON 示例:
1) 反序列化示例。在头文件中:
在实现文件中:
2) 序列化示例。在实现文件中:
这是 Serialize 示例的 NSLog 结果:
Finally we can solve this problem easily using JSONModel. This is the best method so far. JSONModel is a library that generically serialize/deserialize your object based on Class. You can even use non-nsobject based for property like
int
,short
andfloat
. It can also cater nested-complex JSON.Considering this JSON example:
1) Deserialize example. in header file:
in implementation file:
2) Serialize Example. In implementation file:
And this is NSLog result from Serialize example:
听起来您正在寻找一个序列化库,可以让您将自己的自定义类的对象转换为 JSON,然后将它们重新构造回来。属性列表类型(NSArray、NSNumber 等)的序列化已经存在于第 3 方库中,甚至内置于 OS X 10.7 和 iOS 5 中。
所以,我认为答案基本上是“否”。一两个月前,我在 cocoa-dev 邮件列表上问了这个确切的问题,最接近我的热门来自 Mike Abdullah,他指出了他编写的一个实验库:
https://github.com/mikeabdullah/KSPropertyListEncoder
这会将对象存档到内存中属性列表,但正如我所说已经有 API 可将它们转换为 JSON。
还有一个名为 Objectify 的商业应用程序声称能够执行类似的操作:
http://tigerbears.com/objectify/< /a>
我最终可能会在我的 CouchCocoa 库中实现您所要求的内容,但我还没有深入研究该任务。
https://github.com/couchbaselabs/CouchCocoa
It sounds like you're looking for a serialization library that can let you convert objects of your own custom classes into JSON, and then reconstitute them back. Serialization of property-list types (NSArray, NSNumber, etc.) already exists in 3rd party libraries, and is even built into OS X 10.7 and iOS 5.
So, I think the answer is basically "no". I asked this exact question a month or two ago on the cocoa-dev mailing list, and the closest I got to a hit was from Mike Abdullah, pointing to an experimental library he'd written:
https://github.com/mikeabdullah/KSPropertyListEncoder
This archives objects to in-memory property lists, but as I said there are already APIs for converting those into JSON.
There's also a commercial app called Objectify that claims to be able to do something similar:
http://tigerbears.com/objectify/
It's possible I'll end up implementing what you're asking for as part of my CouchCocoa library, but I haven't dived into that task yet.
https://github.com/couchbaselabs/CouchCocoa
借助 NSDictionary、NSArray 和 NSJSONSerialization,您可以轻松地向 NSObject 类添加 JSON 功能。
序列化:
只要看示例就很容易理解。
为 NSObject 类添加 JSON 功能:-
JSON 字符串生成器:-
在 iOS 5 中,Apple 引入了 NSJSONSerialization,用于解析 JSON 字符串,因此我们可以使用它生成 JSON 字符串。
转向 Apple 的实施总是使用起来更安全,因为您可以保证它将得到维护并保持最新。
使用方法:-
参考
反序列化:
这是将反序列化数据放入 NSDictionary 或NSArray 然后将其分配给类属性。
我确信使用上面使用的方法和想法,您可以序列化 &轻松反序列化复杂的 json。
You can easily add JSON capability to NSObject class with the help of NSDictionary,NSArray and NSJSONSerialization
Serialization:
Just see the example it will be very easy to understand.
Adding JSON Capability to NSObject Class:-
JSON String Generator:-
In iOS 5, Apple introduced NSJSONSerialization, for parsing JSON strings so by using that we will generate JSON string.
Moving towards Apple’s implementation is always safer to use since you have the guarantee that it will be maintained and kept up to date.
Way to use:-
Reference
Deserialization:
It's usual way of getting the deserialized data into NSDictionary or NSArray then assign it to class properties.
I am sure using the methods and ideas used above you can serialize & deserialize complex json easily.
您可能想尝试 JTObjectMapping。他们的描述:
它非常小(与 RestKit 不同)并且运行良好。
You may want to try JTObjectMapping. Their description:
It's very small (unlike RestKit) and works great.
使用 RestKit 库的对象映射系统可以实现这一点。
http://restkit.org/
This is possible using the RestKit library's object mapping system.
http://restkit.org/
我有一个简单的模型类,我想将其转换为 JSON 对象。
为此,我在模型类中添加了一个“jsonData”方法:
该方法将模型属性转换为基础对象(int 数字转换为 NSNumber 对象等)
然后用这些对象和相应的键(也是后来的 JSON 键)填充字典。
在(可选)检查有效性后,使用 NSJSONSerialization 创建 JSON 数据对象
类“dataWithJSONObject”方法并返回。
}
I have a simple model class, which I wanted to turn into a JSON-Object.
For this purpose I added a „jsonData“-method to my model class:
The method turns the model properties into foundation objects (int numbers into NSNumber objects etc.)
Then a dictionary is populated with these objects and the corresponding keys (also the later JSON keys).
After an (optional) check for validity, the JSON data object ist created with the NSJSONSerialization
class „dataWithJSONObject“ method and returned.
}
你是这个意思吗? ;)
http://developer.apple.com/library/mac/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40010946
Did you mean this? ;)
http://developer.apple.com/library/mac/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40010946