2012-11-01 26 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

您是否正確鏈接該類別?對我有點看起來像你是一個類別丟失