1
//
//  main.m
//  ASADeepDictionary
//
//  Created by AndrewShmig on 3/10/13.
//  Copyright (c) 2013 AndrewShmig. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "ASADeepDictionary.h"

int main(int argc, const char * argv[])
{
 @autoreleasepool {

NSDictionary *dic = @{@"key":@"value"};
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];

[archiver encodeObject:dic];
[archiver finishEncoding];

id json = [NSJSONSerialization
           dataWithJSONObject:data
           options:0
           error:nil];

NSLog(@"%@", json);

 }

 return 0;
}

私が得ているエラーは次のとおりです。

2013-03-10 19:48:13.420 ASADeepDictionary[9451:303] *** Terminating app due to uncaught    exception 'NSInvalidArgumentException', reason: '*** +[NSJSONSerialization dataWithJSONObject:options:error:]: Invalid top-level type in JSON write'
*** First throw call stack:
 (
    0   CoreFoundation                      0x00007fff89fb70a6 __exceptionPreprocess + 198
    1   libobjc.A.dylib                     0x00007fff88fac3f0 objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff89fb6e7c +[NSException raise:format:] + 204
    3   Foundation                          0x00007fff848bb49d +[NSJSONSerialization dataWithJSONObject:options:error:] + 249
    4   ASADeepDictionary                   0x0000000100001051 main + 321
    5   libdyld.dylib                       0x00007fff84bfd7e1 start + 0
    6   ???                                 0x0000000000000001 0x0 + 1
4

2 に答える 2

2

最初の引数は、ではなく、NSDictionaryまたはである必要があります。あなたはこの方法が何をするのか誤解しているようです。渡されたオブジェクトをシリアル化し、解析しません。NSArrayNSData

于 2013-03-10T15:57:34.990 に答える
0
NSDictionary *dic = @{@"key":@"value"};

id json = [NSJSONSerialization
           dataWithJSONObject:dic
           options:0
           error:nil];

NSLog(@"%@", [NSString stringWithUTF8String:[json bytes]]);
于 2013-03-10T15:57:19.087 に答える