2012-11-01 28 views
0

我试图解析NSArrayJSON,但我得到了以下错误:SBJsonParser无法解析的NSArray

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM JSONRepresentation]: unrecognized selector sent to instance 0xa93e460' * First throw call stack: (0x21f1012 0x1feae7e 0x227c4bd 0x21e0bbc 0x21e094e 0x3445a 0x33ecc 0x26a453f 0x26b6014 0x26a72e8 0x26a7450 0x95e22e12 0x95e0acca) libc++abi.dylib: terminate called throwing an exception

我已经包括了从SBJson_3.1.1 /类目录中的所有类。
这是代码:

NSMutableArray* arr = ...get array 
NSString* jsonArr = [arr JSONRepresentation]; // here I get error 

当我做这个简单的字符串数组它的工作原理:

NSData *jsonData = [NSJSONSerialization arr 
                 options:NSJSONWritingPrettyPrinted error:nil]; 
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 

但是我的数组包含对象(人)的名单可能有问题。

我使用的项目,而不是人,就像例如
Item.h

@interface Item : NSObject 
{ 
    BOOL IsOpen; 
    NSString* Description; 
} 

@property int ItemId; 
@property int SequenceId; 
@property BOOL IsOpen; 
@property NSString* Description; 

- (id) proxyForJson; 

@end 

Item.m

@implementation Item 
@synthesize ItemId; 
@synthesize SequenceId; 
@synthesize Description; 
@synthesize IsOpen; 

- (id) proxyForJson { 

    return [NSDictionary dictionaryWithObjectsAndKeys: 
      [NSString stringWithFormat:@"%i", ItemId], @"ItemId", 
      SequenceId, @"SequenceId", 
      Description, @"Description", 
      IsScanned, @"IsOpen", 
      nil ]; 
} 
@end 



UPDATE

学生例如
我试着做一个单独的项目。我从sbjson框架的classes目录复制到新项目。这是代码:

#import "SBJson.h" 

@interface Student : NSObject 
{ 
    NSString *name; 
    NSInteger sid; 

    NSString *email; 
} 
@property NSString *name; 
@property NSInteger sid; 

@property NSString *email; 

- (id) proxyForJson; 

@end 

@implementation Student 
@synthesize name; 
@synthesize sid; 

@synthesize email; 

- (id) proxyForJson{ 
    return [NSDictionary dictionaryWithObjectsAndKeys: 
      name, @"student_name", 
      [NSNumber numberWithInt:sid], @"student_id", 
      email, @"email", 
      nil ]; 
} 

@end 

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

    Student* s1 = [[Student alloc]init]; 
    s1.name = @"student 1"; 
    s1.sid = 45; 
    s1.email = @"[email protected]"; 

    Student* s2 = [[Student alloc]init]; 
    s2.name = @"student 2"; 
    s2.sid = 46; 
    s2.email = @"[email protected]"; 

    [studentArray addObject:s1]; 
    [studentArray addObject:s2]; 

    NSString *jsonString = [studentArray JSONRepresentation]; 

    NSLog(@"%@", jsonString); 

我再次得到错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM JSONRepresentation]: unrecognized selector sent to instance 0x741b100'

+1

请看到几个类似的问题:http://stackoverflow.com/questions/9420126/sbjson-parsing-nsstring-to-nsdictionary&http://stackoverflow.com/问题/ 11937587/IOS-的Objective-C-JSON字符串到NSDictionary中的异常与此评论:http://stackoverflow.com/a/5214540/5950 –

+1

我昨晚谷歌,但没有什么帮助包括栈的问题;(我刚刚更新了我的问题有关问题的详细信息。 – 1110

+0

你错过定义'JSONRepresentation'。 –

回答

0

SBJson不支持序列化的用户定义的类如果没有援助。但是,如果您在Person类中实施-proxyForJson方法(例如here),它应该可以工作。

如果您使用的是最近的Xcode,则下面的代码应该可以工作。标题:

@interface Item : NSObject 
@property int ItemId; 
@property int SequenceId; 
@property BOOL IsOpen; 
@property(copy) NSString* Description; 
- (id) proxyForJson; 
@end 

实现:

@implementation Item 
- (id) proxyForJson { 
    return @{ @"ItemId": @(self.ItemId), 
       @"SequenceId": @(self.SequenceId), 
       @"Description": self.Description, 
       @"IsOpen": @(self.IsOpen) 
       }; 
} 
@end 

这应该让SBJson连载的项目对象NSDictionaries。但是,SBJson确实支持将而不是解析为自定义对象。所以你总是会以字典的形式回到原来的位置。我不知道任何提供绑定到自定义类型的Objective-C JSON解析器。

+0

我已更新我的问题,并设置赏金可以检查吗? – 1110

+0

我仍然得到错误:(:终止应用程序由于未捕获的异常“NSInvalidArgumentException”,原因:“ - [__ NSArrayM JSONRepresentation]:无法识别的选择发送到实例0xf73c5c0” – 1110

0

我建议您阅读this thread的前两条评论。如果这些没有帮助,那么您很可能没有正确安装库。尝试从您的项目中删除SBJSON文件,然后阅读它们,确保将它们添加到您的目标中。另外,请确保您将SBJSON标头导入您的班级。

我建议您尝试在NSString对象的数组上使用JSONRepresentation。如果框架安装正确,这肯定会起作用。通过这种方式,您可以缩小它是否是安装问题,或者是否是您的自定义类的问题。

+0

我已经更新的问题,我试图让所有从开始的一步步骤 – 1110

+0

我在回复中添加了一条建议。 –

0

查看下面摘录自Working with JSON in iOS 5 Tutorial 这主要是为了生成JSON。

//build an info object and convert to json 
NSDictionary* info = [NSDictionary dictionaryWithObjectsAndKeys: 
[loan objectForKey:@"name"], 
@"who", 
[(NSDictionary*)[loan objectForKey:@"location"] 
objectForKey:@"country"], 
@"where", 
[NSNumber numberWithFloat: outstandingAmount], 
@"what",nil]; 

//convert object to data 
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:info options:NSJSONWritingPrettyPrinted error:&error]; 

现在,不同之处在于使用NSDictionary中,并转化到这一点JSON 数据。尝试以上述方式形成JSON并检查问题是否存在。

0

您是否正确链接该类别?对我有点看起来像你是一个类别丢失