NSJSONSerialization不解析NSManagedObject

时间:2013-11-01 15:58:42

标签: ios json ios7 nsjsonserialization

我需要生成POST数据的JSON并获得异常。

这是带有“records”键的字典,其中包含NSManagedObjects数组。

在将其解析为json时似乎抛出异常。

NSData *postData = [NSJSONSerialization dataWithJSONObject:[super requestData] options:0 error:&error];

enter image description here

失败,出现以下异常。知道为什么会这样吗?

'NSInvalidArgumentException', reason: 'Invalid type in JSON write (FTRecord)'
*** First throw call stack:
(
    0   CoreFoundation                      0x01aae5e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x018318b6 objc_exception_throw + 44
    2   CoreFoundation                      0x01aae3bb +[NSException raise:format:] + 139
    3   Foundation                          0x015760f0 _writeJSONValue + 771
    4   Foundation                          0x0157a470 ___writeJSONArray_block_invoke + 129
    5   CoreFoundation                      0x01b2b05d __53-[__NSArrayI enumerateObjectsWithOptions:usingBlock:]_block_invoke + 61
    6   CoreFoundation                      0x01b2af92 -[__NSArrayI enumerateObjectsWithOptions:usingBlock:] + 258
    7   CoreFoundation                      0x01aa80a5 -[NSArray enumerateObjectsUsingBlock:] + 53
    8   Foundation                          0x0157a381 _writeJSONArray + 275

FTRecord

@interface FTRecord : NSManagedObject

@property (nonatomic) BOOL isRealEntry;
@property (nonatomic) NSTimeInterval timestamp;
@property (nonatomic, retain) NSString * notes;
@property (nonatomic) double orderingValue;
@property (nonatomic) int16_t rating;
@property (nonatomic) NSTimeInterval recordDate;

@end

1 个答案:

答案 0 :(得分:1)

从文档中,NSManagedObject不符合NSJSONSerialization的有效对象标准:

  

可以转换为JSON的对象必须具有以下内容   属性:

     

顶级对象是NSArray或NSDictionary。所有对象都是   NSString,NSNumber,NSArray,NSDictionary或NSNull的实例。所有   字典键是NSString的实例。数字不是NaN或   无穷大。

您可以致电isValidJSONObject:

进行测试