0

に解析しようとしNSArrayJSONいますが、次のエラーが発生します。

* 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: 呼び出されたときに例外をスローして終了します

SBJson_3.1.1/Classesディレクトリのすべてのクラスを含めました。
これはコードです:

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];

しかし、私の配列にはオブジェクト (Person) のリストが含まれているため、問題がある可能性があります。 例のItem.h

として、 person の代わりに Item を使用します 。

@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 = @"test@test.com";

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

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

    NSString *jsonString = [studentArray JSONRepresentation];

    NSLog(@"%@", jsonString);

そして再びエラーが発生します:

キャッチされない例外 'NSInvalidArgumentException' が原因でアプリを終了しています。理由: '-[__NSArrayM JSONRepresentation]: 認識されないセレクターがインスタンス 0x741b100 に送信されました'

4

4 に答える 4

0

iOS5チュートリアルでのJSONの 操作からの次の抜粋を確認してください 。これは主に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を作成してみて、問題が解決するかどうかを確認してください。

于 2012-11-07T08:58:36.183 に答える
0

カテゴリを正しくリンクしていますか?私には、カテゴリが欠けているように見えます

于 2012-11-09T22:00:46.363 に答える
0

SBJson は、支援なしではユーザー定義クラスのシリアル化をサポートしていません。-proxyForJsonただし、 Person クラスにメソッドを実装すると(例はこちら)、動作するはずです。

最近の 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 が Item オブジェクトを NSDictionaries にシリアル化できるようになります。ただし、SBJson はJSON のカスタム オブジェクトへの解析をサポートしていません。したがって、これは常に辞書形式で返されます。カスタム型へのバインディングを提供する Objective-C JSON パーサーを知りません。

于 2012-11-03T16:30:58.637 に答える