且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将Objective-C对象序列化和反序列化为JSON

更新时间:2022-12-10 12:40:13

听起来您正在寻找一个序列化库,该库可以让您将自己的自定义类的对象转换为JSON,然后将其重新构造.属性列表类型(NSArray,NSNumber等)的序列化已存在于第三方库中,甚至内置于OS X 10.7和iOS 5中.

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.

所以,我认为答案基本上是否".我在一两个月前在cocoa-dev邮件列表上问了这个确切的问题,最接近我的是迈克·阿卜杜拉(Mike Abdullah),他指向他写的一个实验库:

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

这会将对象存档到内存中的属性列表,但是正如我所说的,已经有将这些对象转换为JSON的API.

This archives objects to in-memory property lists, but as I said there are already APIs for converting those into JSON.

还有一个名为Objectify的商业应用程序,它声称能够执行类似的操作:

There's also a commercial app called Objectify that claims to be able to do something similar:

http://tigerbears.com/objectify/

作为我的CouchCocoa库的一部分,我最终可能会实现您的要求,但我尚未深入研究该任务.

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