Objective C将集合序列化为JSON / XML

时间:2012-06-01 14:42:26

标签: objective-c json xml-serialization

我有一个类似这样的课程:

@interface AISlideItem: NSObject <NSCoding>
{
    NSString*  PlaceHolderName;
    NSInteger PlaceHolderID;
}
@property (nonatomic, strong) NSString* PlaceHolderName;
@property (nonatomic) NSInteger 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

我从这些类创建一个复杂的集合,如下所示:

  NSMutableArray* l = [[NSMutableArray alloc]init ];

        AITitleSlideItem* tis1 = [[AITitleSlideItem alloc]init];
        [tis1 setPlaceHolderName:@"I don't think we're using this..."];
        [tis1 setPlaceHolderID:1];
        [tis1 setTitle:@"This is a title"];

        AITextSlideItem* tes1 = [[AITextSlideItem alloc]init];
        [tes1 setPlaceHolderName:@"I don't think we're using this..."];
        [tes1 setPlaceHolderID:2];
        [tes1 setText:@"This is the description"];

        AISlideContent* slide1 = [[AISlideContent alloc]init];
        [slide1 setLayoutName:@"Title content"];
        [[slide1 Items]addObject:tis1];
        [[slide1 Items]addObject:tes1];


        [l addObject:slide1];
        [l addObject:slide1];

我想将NSMutableArray l序列化为任何可移植格式,如json或xml。 你能发一些代码吗?

此致

佐利

1 个答案:

答案 0 :(得分:0)

经过进一步调查,我发现没有更适合我的需求。 我决定学习更多关于JSON的知识,并创建一个手动创建JSON结构的函数。即使耗时2-3小时,也值得付出全部努力。