【问题标题】:Serialize custom object to JSON which contains NSMutableArray将自定义对象序列化为包含 NSMutableArray 的 JSON
【发布时间】:2013-10-27 21:25:33
【问题描述】:

我正在尝试序列化我的 Cart 对象,其中包含 NSMutableArray 的项目但得到:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (Item)'

如果我了解它应该如何工作,我需要创建一个字典数组以使 NSJSONSerialization 正常工作。这不是我在下面做的吗?

我的购物车.h:

@interface Cart : BaseObject

@property (nonatomic, strong) NSString *comp;
@property (nonatomic, strong) NSString *sono;
@property (nonatomic, strong) NSString *cust;
@property (nonatomic, strong) NSString *scus;
@property (nonatomic, strong) NSString *cnid;
@property (nonatomic, strong) NSString *dldt;
@property (nonatomic, strong) NSString *whse;
@property (nonatomic, strong) NSString *pono;
@property (nonatomic, strong) NSString *pon2;
@property (nonatomic, strong) NSString *emad;
@property (nonatomic, strong) NSString *pkin;
@property (nonatomic, strong) NSString *comt;
@property (nonatomic, strong) NSString *rtin;
@property (nonatomic, strong) NSString *lbfg;
@property (nonatomic, strong) NSString *containsOpenPriced;
@property (nonatomic, strong) NSString *totalProductAmount;
@property (nonatomic, strong) NSMutableArray *items;
@property (nonatomic) BOOL *isSubmitting;

@end

我的购物车.m:

@implementation Cart

@synthesize comp;
@synthesize sono;
@synthesize cust;
@synthesize scus;
@synthesize cnid;
@synthesize dldt;
@synthesize whse;
@synthesize pono;
@synthesize pon2;
@synthesize emad;
@synthesize pkin;
@synthesize comt;
@synthesize rtin;
@synthesize lbfg;
@synthesize containsOpenPriced;
@synthesize totalProductAmount;
@synthesize items;

- (id) initWithDictionary:(NSDictionary *)dictionary {
    self = [super init];
    if (self) {
        self.comp = dictionary[@"comp"];
        self.sono = dictionary[@"sono"];
        self.cust = dictionary[@"cust"];
        self.scus = dictionary[@"scus"];
        self.cnid = dictionary[@"cnid"];
        self.dldt = dictionary[@"dldt"];
        self.whse = dictionary[@"whse"];
        self.pono = dictionary[@"pono"];
        self.pon2 = dictionary[@"pon2"];
        self.emad = dictionary[@"emad"];
        self.pkin = dictionary[@"pkin"];
        self.comt = dictionary[@"comt"];
        self.rtin = dictionary[@"rtin"];
        self.lbfg = dictionary[@"lbfg"];
        self.containsOpenPriced = dictionary[@"containsOpenPriced"];
        self.totalProductAmount = dictionary[@"totalProductAmount"];

        NSArray *itemsArray = dictionary[@"items"];
        NSMutableArray *itemsMutableArray = [[NSMutableArray alloc]init];
        for (NSDictionary *itemDictionary in itemsArray) {
            Item *item = [[Item alloc] initWithDictionary:itemDictionary];
            [itemsMutableArray addObject:item];
        }
        self.items = itemsMutableArray;
    }

    return self;
}

@end

我的对象序列化代码:

NSMutableArray *itemsToSerialize = [[NSMutableArray alloc] init];
for (Item *item in cart.items) {
    NSMutableDictionary *itemDict = [[NSMutableDictionary alloc] init];
    [itemDict setObject:item forKey:@"item"];
    [itemsToSerialize addObject:item];
}

NSMutableDictionary *data = [@{
        @"comp" : cart.comp,
        @"rtin" : cart.rtin,
        @"pono" : cart.pono,
        @"pon2" : cart.pon2,
        @"totalProductAmount" : totalProductAmount,
        @"sono" : cart.sono,
        @"emad" : cart.emad,
        @"lbfg" : lbfg,
        @"pkin" : cart.pkin,
        @"containsOpenPriced" : containsOpenPriced,
        @"cnid" : cart.cnid,
        @"whse" : cart.whse,
        @"scus" : cart.scus,
        @"dldt" : cart.dldt,
        @"cust" : cart.cust,
        @"comt" : cart.comt,
        @"items": itemsToSerialize
} mutableCopy];

NSString *command = @"shoppingCart.update";
NSMutableDictionary *request = [@{
        @"command" : command,
        @"comp" : cart.comp,
        @"cnid" : sessionController.operator.cnid,
        @"cust" : cart.cust,
        @"data" : data
} mutableCopy];

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:request options:kNilOptions error:nil];

这会死在上面的NSJSONSerialization 行。我错过了什么?

【问题讨论】:

  • 旁注:不要使用@synthesize,不再需要了。

标签: ios objective-c json nsjsonserialization


【解决方案1】:

我知道这有点晚了,但也许这门课可以简化你的编码:

它是一个将自定义 NSObject 转换为 JSON 可读对象(NSArray 或 NSDictionary)的类。试试看。它具有跳过属性并将属性类型从字符串更改为 NSInteger 或 Float 的能力,它还可以更改最终输出的属性名称让我们说

CustomObject *X;
    X.property1 = @"Test";

//and the output JSON readable format you want tot change X to Y

//CustomObject converted to readable format
   {Y = @"Test"}

这里是班级Disassembler

【讨论】:

    【解决方案2】:

    NSJSONSerialization 仅适用于数组(NSArrayNSMutableArray)、字典(NSDictionaryNSMutableDictionary、字符串(NSStringNSMutableString)和 NSNumber

    常见的机制是在您的类上创建一个- (NSDictionary *)serialize 方法,该方法将其所有值复制到字典中以传递给NSJSONSerialization。然后实现- (id)initFromSerialization:(NSDictionary *)serialization 反序列化对象。

    【讨论】:

      【解决方案3】:

      这一行:[itemsToSerialize addObject:item]; 应该是 [itemsToSerialize addObject:itemDict];。结果是您尝试序列化项目本身的数组,这会导致您看到的异常。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-08-17
        • 2010-10-22
        • 1970-01-01
        • 1970-01-01
        • 2015-07-29
        • 2011-10-20
        • 1970-01-01
        相关资源
        最近更新 更多