2012-05-08 60 views
0

我想在我的应用程序中使用JSON,我从未使用过它。我有一个这样的对象:JSON结构表示

@interface Selection : NSManagedObject 

@property (nonatomic, retain) NSString * book_id; 
@property (nonatomic, retain) NSString * contenu; 
@property (nonatomic, retain) NSNumber * page_id; 
@property (nonatomic, retain) NSNumber * nbrOfOccurences; 
@property (nonatomic, retain) NSString * next; 
@property (nonatomic, retain) NSString * previous; 

@end

我想构建JSON同步到我的服务器。使用我的对象"Selection"的结构是什么,即:如何以JSON格式表示"Selection"的许多对象?感谢您的回答。

回答

0

这样做:

NSArray *objects = [NSArray arrayWithObjects:book_id, 
              contenu, 
              page_id, 
              nbrOfOccurences, 
              next, 
              previous, 
              nil]; 
NSError *error = nil; 
NSData *jsonData = [JSONSerializer dataWithJSONObject:objects 
               options:0 
               error:&error]; 

现在jsonData包含objects阵列的JSON编码表示。为了让他们回来,扭转过程:

NSArray *objects = [JSONSerializer JSONObjectWithData:jsonData 
               options:0 
               error:&error];