【问题标题】:Encoding custom classes using yajl-objc使用 yajl-objc 对自定义类进行编码
【发布时间】:2010-09-02 15:34:04
【问题描述】:

总结。基于一些benchmarks,我为我的iPhone JSON 解析器选择了yajl-objc。我正在使用任意自定义类(一个 NSNumber 和两个 NSString 属性)对其进行测试。如果我创建了一个键值对匹配类属性的 NSDictionary,我可以用[dictionary yajl_JSON] 对字典进行编码。当我尝试使用[custom yajl_JSON] 直接对自定义类的实例进行编码时,我得到了这个编译器错误:

Terminating app due to uncaught exception 'YAJLParsingUnsupportedException', reason: 'Object of type (Custom) must implement dataUsingEncoding: to be parsed'。

即使我实现了- (id)JSON(如 yajl-objc readme 中所建议的那样),我也得到了错误。我知道我的代码而不是库是问题所在。我就是想不通我做错了什么。

详细信息。我的 Custom.h:

#import 
@interface Custom : NSObject {

    NSString *name;
    NSNumber *number;
    NSString *text;
}

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSNumber *number;
@property (nonatomic, copy) NSString *text;

@end

在 Custom.m 中,我根据 yajl-obj 文档定义了一个 - (id)JSON 方法:

- (id)JSON 
{
    NSDictionary *jsonDictionary = 
        [[[NSMutableDictionary alloc] init] autorelease];
    [jsonDictionary setValue:name forKey:@"name"];
    [jsonDictionary setValue:number forKey:@"number"];
    [jsonDictionary setValue:text forKey:@"text"];
    return jsonDictionary;
}

创建一个键值匹配的字典并对其进行编码可以正常工作:

    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:self.custom.name forKey:@"name"];
    [dict setValue:self.custom.number forKey:@"number"];
    [dict setValue:self.custom.text forKey:@"text"];
    NSString *JSONString = [dict yajl_JSON];

但是当我打电话给NSString* JSONString = [custom yajl_JSON];这是我得到的编译器错误和堆栈跟踪:

*** Terminating app due to uncaught exception 'YAJLParsingUnsupportedException', reason: 'Object of type (Custom) must implement dataUsingEncoding: to be parsed'
*** Call stack at first throw:
(
 0   CoreFoundation                      0x0266bb99 __exceptionPreprocess + 185
 1   libobjc.A.dylib                     0x027bb40e objc_exception_throw + 47
 2   CoreFoundation                      0x02624238 +[NSException raise:format:arguments:] + 136
 3   CoreFoundation                      0x026241aa +[NSException raise:format:] + 58
 4   TestCustomFileFormat               0x00005151 -[NSObject(YAJL) yajl_JSONWithOptions:error:] + 206
 5   TestCustomFileFormat               0x00005212 -[NSObject(YAJL) yajl_JSON:] + 49
 6   TestCustomFileFormat               0x00005249 -[NSObject(YAJL) yajl_JSON] + 49
 7   TestCustomFileFormat               0x00002eba -[TestViewController loadInstruction] + 758
 8   TestCustomFileFormat               0x00002aa5 -[TestViewController viewDidLoad] + 77
 9   UIKit                               0x0037585a -[UIViewController view] + 179
 10  TestCustomFileFormat               0x00002377 -[TestCustomFileFormatAppDelegate application:didFinishLaunchingWithOptions:] + 112
 11  UIKit                               0x002cc6a7 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1163
 12  UIKit                               0x002ceb30 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 346
 13  UIKit                               0x002d8b6c -[UIApplication handleEvent:withNewEvent:] + 1958
 14  UIKit                               0x002d12bc -[UIApplication sendEvent:] + 71
 15  UIKit                               0x002d613f _UIApplicationHandleEvent + 7672
 16  GraphicsServices                    0x02f4b81e PurpleEventCallback + 1578
 17  CoreFoundation                      0x0264cff4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
 18  CoreFoundation                      0x025ad807 __CFRunLoopDoSource1 + 215
 19  CoreFoundation                      0x025aaa93 __CFRunLoopRun + 979
 20  CoreFoundation                      0x025aa350 CFRunLoopRunSpecific + 208
 21  CoreFoundation                      0x025aa271 CFRunLoopRunInMode + 97
 22  UIKit                               0x002ce3ed -[UIApplication _run] + 625
 23  UIKit                               0x002da272 UIApplicationMain + 1160
 24  TestCustomFileFormat               0x000022e4 main + 102
 25  TestCustomFileFormat               0x00002275 start + 53
)
terminate called after throwing an instance of 'NSException'

作为回应,我尝试遵循 NSCoding,使用 NSKeyedArchiver 创建类实例的 NSMutableData 表示并调用 [data yajl_JSON],但这也不起作用。

我知道我错过了一些简单的东西,但我太愚蠢了,无法弄清楚。

【问题讨论】:

    标签: iphone objective-c json yajl


    【解决方案1】:

    你做错了。要从对象创建 JSON 字符串,请使用 yajl_JSONString。 yail_JSON 用于获取字符串(或 NSData 对象或可以存档的东西)并将其解析为对象。

    【讨论】:

    • 斯文,谢谢。我知道这很愚蠢。现在效果很好。
    猜你喜欢
    • 2011-08-11
    • 2013-02-01
    • 2010-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    • 2017-11-23
    • 1970-01-01
    相关资源
    最近更新 更多