2012-02-23 60 views
2

我试图解析包含JSON数据到一个NSDictionary使用SBJson 3.0.4一个NSString,但是当我这样做,我得到这个错误:SBJSON解析的NSString的NSDictionary

"WebKit discarded an uncaught exception in the webView:shouldInsertText:replacingDOMRange:givenAction: delegate: -[__NSCFString JSONValue]: unrecognized selector sent to instance 0x6ab7a40"

至于我知道(这不是很远),我得到的JSON是有效的,所以我不知道为什么会发生这种情况。我的代码编译罚款太... 这就是:

NSString *tempURL = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@&sensor=true",userInput.text]; 
NSURL *url = [NSURL URLWithString:tempURL]; 
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url 
              cachePolicy:NSURLRequestReturnCacheDataElseLoad 
             timeoutInterval:30]; 
// fetch the JSON response 
NSData *urlData; 
NSURLResponse *response; 
NSError *error; 

// make the synchronous request 
urlData = [NSURLConnection sendSynchronousRequest:urlRequest 
           returningResponse:&response 
              error:&error]; 

// construct a String around the Data from the response 
NSString *data = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding]; 
NSDictionary *feed = [data JSONValue]; 
+0

打印字符串'data'的值。将数据内容复制到JSONLint.com的工具中,并查看它是否为有效的JSON。让我们知道发生了什么。 – Jim 2012-02-23 19:52:33

+0

它说,它是有效的 – 2012-02-23 20:07:46

回答

9

错误信息的重要组成部分,是这样的:

-[__NSCFString JSONValue]: unrecognized selector sent to instance 0x6ab7a40 

__NSCFString类是NSString接口私有实现类,所以你可以假装说NSString

因此,我们看到您正在将JSONValue消息发送到NSString,并且NSString表示它无法识别该选择器。 SBJson库将JSONValue方法添加到NSStringusing a category

所以我推断你没有将NSObject+SBJson.o链接到你的应用程序中。如果您将SBJson源文件复制到您的应用程序中,请确保已将其复制到NSObject+SBJson.m中,并确保它已包含在目标的“编译源”编译阶段。

如果你建立了一个SBJson库,并链接您的应用程序,你可能需要将-ObjC标志添加到您的链接器选项,甚至-all_load标志。

+0

我忘了将它添加到编译源代码,但是当我这样做,我现在得到两个错误: '对于架构i386的未定义符号: “_OBJC_CLASS _ $ _ SBJsonWriter”,从引用: objc-类 - 引用自: objc-class-ref in NSObject + SBJson.o ld:symbol(s)not found for architecture i386 clang:error:linker command failed with退出代码1(使用-v查看调用)' – 2012-02-23 20:22:48

+4

您需要将**所有**的SBJson'.m'文件添加到“编译源”构建阶段。 – 2012-02-23 20:23:51

+0

aaaaaahhhhhh好的。哇谢谢!它现在完美运行!谢谢! – 2012-02-23 20:26:31