Objective C序列化复杂对象列表

时间:2012-06-01 09:37:21

标签: objective-c

我有一个类似的列表:

@interface AISlideItem: NSObject
{
    NSString*  PlaceHolderName;
    NSUInteger PlaceHolderID;
}
@property (nonatomic, strong) NSString* PlaceHolderName;
@property (nonatomic) NSUInteger PlaceHolderID;

@end

@interface AITitleSlideItem : AISlideItem
{
    NSString* Title;
}
@property (nonatomic, strong) NSString* Title;
@end

@interface AIParagraphSlideItem : AISlideItem
{
    NSMutableArray* Paragraphs;
}
@property (nonatomic, strong) NSMutableArray* Paragraphs;
@end

@interface AITextSlideItem : AISlideItem
{
    NSString* Text;
}
@property (nonatomic, strong) NSString* Text;
@end

@interface AIPictureSlideItem : AISlideItem
{
    NSMutableData* Picture;
}
@property (nonatomic, strong) NSMutableData* Picture;
@end

@interface AISlideContent : NSObject
{
    NSString*       LayoutName;
    NSMutableArray* Items;
}
@property (nonatomic,strong) NSString* LayoutName;
@property (nonatomic,strong) NSMutableArray* Items;
@end

@interface ContentParaghraph : NSObject
{
    NSInteger Level;
    NSString* Text;
}
@property (nonatomic) NSInteger Level;
@property (nonatomic, strong) NSString* Text;
@end

我想将持有AISlideContent对象的NSMutableArray序列化为json。 json中的每个项目名称都应与变量名称相同。

我该怎么做?

这是一个JSON示例:

{
  d: [
     {
          Items: [
          {
              placeHolderName: "1",
              Title: "Title Slide"
          },
          {
              placeHolderName: "2",
              Paragraphs: [
                 {
                     Level: 0,
                     Text: "Title content"
                 }
              ]
          }
       ],
       LayoutName: "Title"
     },
     {
         Items: [
         {
              placeHolderName: "1",
              Title: "Slide 2"
     },
     {
              placeHolderName: "2",
              Paragraphs: [
              {
                 Level: 0,
                 Text: "Line 1"
              },
              {
                 Level: 0,
                 Text: "Line 2"
              },
              {
                 Level: 1,
                 Text: "Line 2 indented"
              },
              {
                 Level: 2,
                 Text: "Line 3 more indented"
              },
              {
                 Level: 0,
                 Text: "Line 4"
              }
              ]
     }
      ],
      LayoutName: "Title and Content"
     },
     {
           Items: [
           {
                placeHolderName: "1",
                Title: "Slide 3"
           },
           {
                placeHolderName: "3",
                Picture: [

                ]
           }
       ],
       LayoutName: "Picture above Caption"
      }
      ]
}

提前致谢!

佐利

P.S。我启用了ARC

2 个答案:

答案 0 :(得分:2)

您必须遵循NSCoding协议并覆盖encodeObjectdecodeObject方法。

Here是一个样本。

答案 1 :(得分:1)

Objective-C JSON库定义Objective-C类和相应的JSON元素之间的映射。例如,NSArray将映射到JSON数组,NSDictionary将映射到JSON对象,NSString将映射到JSON字符串,因此强制。

JSON序列化程序的实现将遍历对象层次结构,并使用内省构建Objective-C类类型,或者在内部为可序列化的Objective-C类实现类别,然后调用/调用相应的序列化方法,以便将字符写入流。

现在,让对象层次结构包含具有任意类的对象并尝试运行序列化程序可能会失败,因为它不知道如何序列化NSDate例如。

此问题的一个可能解决方案是查看您正在使用的JSON库的文档,并确定是否以及如何实现此功能。 AFAIK,Cocoa的内置NSJSONSerialization无法做到这一点。

最近的 JSONKit 可能对您有用,它使用回调机制。不过,我看起来并不太深入。

另一方面,

JPJson库使用Category方法。您只需为自定义类定义一个Category,分别为要序列化的内置Cocoa类(例如NSDate)并实现协议。一旦实现(这在JPJson中非常容易),即使在深层嵌套的层次结构中,该类的对象也将被正确序列化,维护其他序列化选项 - 如“漂亮打印”,Unicode编码,字符串转义等。

看看你的例子,用JPJson实现起来似乎很容易。 JPJson包位于GitHub上,还有一个示例,显示如何序列化这些自定义类。

您可以查看https://github.com/couchdeveloper/JPJson,尤其是Sample6。