【问题标题】:iOS objective-c - JSON String to NSDictionary ExceptioniOS 目标 c - NSDictionary 异常的 JSON 字符串
【发布时间】:2012-08-13 15:34:48
【问题描述】:

我对 Objective-C 比较陌生,并且遵循了关于 MapKit 简介的教程(位于 here)。我一直在尝试调试将 JSON 字符串传递给 NSDictionary 的问题。我收到以下错误:

2012-08-13 11:18:30.370 ArrestsPlotter[76578:c07] -[__NSCFString JSONValue]: unrecognized
selector sent to instance 0x73a6400
2012-08-13 11:18:30.372 ArrestsPlotter[76578:c07] *** Terminating app due to uncaught 
exception 'NSInvalidArgumentException', reason: '-[__NSCFString JSONValue]: unrecognized 
selector sent to instance 0x73a6400'
*** First throw call stack:
(0x17b4022 0x131bcd6 0x17b5cbd 0x171aed0 0x171acb2 0x34782 0x35351 0x1c65e 0x17b5e42 
0xda49df 0x178894f 0x16ebb43 0x16eb424 0x16ead84 0x16eac9b 0x1ddd7d8 0x1ddd88a 0x47e626 
0x3403d 0x20f5)
terminate called throwing an exception

我已将其范围缩小到给我问题的这一行:

NSDictionary * root = [responseString JSONValue];

根据 SBJSON 库文档,这是可能的。我认为这与我传递给 NSDictionary 的字符串有关。下面是创建 JSON 字符串的代码:

// 1
MKCoordinateRegion mapRegion = [_mapView region];
CLLocationCoordinate2D centerLocation = mapRegion.center;

// 2
NSString *jsonFile = [[NSBundle mainBundle] pathForResource:@"command" ofType:@"json"];
NSString *formatString = [NSString stringWithContentsOfFile:jsonFile encoding:NSUTF8StringEncoding error:nil];
NSString *json = [NSString stringWithFormat:formatString,
                  centerLocation.latitude, centerLocation.longitude, 0.5*METERS_PER_MILE];

// 3
NSURL *url = [NSURL URLWithString:@"http://data.baltimorecity.gov/api/views/INLINE/rows.json?method=index"];

// 4
ASIHTTPRequest *_request = [ASIHTTPRequest requestWithURL:url];
__weak ASIHTTPRequest *request = _request;

request.requestMethod = @"POST";
[request addRequestHeader:@"Content-Type" value:@"application/json"];
[request appendPostData:[json dataUsingEncoding:NSUTF8StringEncoding]];
// 5
[request setDelegate:self];
[request setCompletionBlock:^{
    NSString *responseString = [request responseString];
    NSLog(@"Response: %@", responseString);
    [self plotCrimePositions:responseString];

字符串被正确填充,并且在终端中显示如下(整个事情很大,所以我只发布一小部分):

2012-08-13 11:18:30.210 ArrestsPlotter[76578:c07] Response: {
"meta" : {
"view" : {
  "id" : "zzzz-zzzz",
  "name" : "Inline View",
  "attribution" : "Baltimore Police Department",
  "attributionLink" : "http://www.baltimorepolice.org/",
  "averageRating" : 0,
  "category" : "Crime",
  "licenseId" : "CC_30_BY",
  "numberOfComments" : 0,
  "oid" : 0,
  "publicationAppendEnabled" : false,
  "publicationStage" : "unpublished",
  "rowsUpdatedAt" : 1338813661,
  "rowsUpdatedBy" : "n22b-663u",
  "signed" : false,
  "tableId" : 354024,
  "totalTimesRated" : 0,
  "viewType" : "tabular",
  "columns" : [ {
    "id" : -1,
    "name" : "sid",
    "dataTypeName" : "meta_data",
    "fieldName" : ":sid",
    "position" : 0,
    "renderTypeName" : "meta_data",
    "format" : {
    }
  }, {
    "id" : -1,
    "name" : "id",
    "dataTypeName" : "meta_data",
    "fieldName" : ":id",
    "position" : 0,
    "renderTypeName" : "meta_data",
    "format" : {
    }
  }

任何帮助将不胜感激。我意识到我使用的教程有点过时了,但我以前从未使用过 JSON,所以我不太确定问题出在哪里。

【问题讨论】:

    标签: objective-c ios json nsdictionary sbjson


    【解决方案1】:

    该问题是由于尝试将消息JSONValue 发送到NSString 中的实例而导致的,该实例不支持此方法。请尝试以下代码

    SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
    NSError *error = nil;
    NSArray *jsonObjects = [jsonParser objectWithString:responseString error:&error];
    

    这应该会给你一个包含响应数据的 NSDictionaries 的 NSArray。

    您也可以尝试在此处复制和粘贴整个 JSON 响应以检查其是否有效

    http://jsonformatter.curiousconcept.com/

    编辑:尽管正如其他一些答案所指出的那样,#importing SBJSON.h 应该使用类别将JSONValue 方法动态添加到NSString 类。在这里解释

    http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/chapters/occategories.html

    【讨论】:

    • 所以这让我找到了答案。我在使用 SBJsonParser 时仍然遇到问题,但我发现我需要将来自 SBJson 的所有 .m 文件包含到构建阶段下的编译源中。现在我原来的方法正在奏效。谢谢!
    【解决方案2】:

    实际上问题不在于缺少标头导入(嗯,这可能是问题的一部分),而是缺少实现 .m 文件。您需要确保 NSObject+SBJson.m 包含在您的项目中,无论是独立的还是作为库的一部分。

    【讨论】:

    • 嗯,它肯定包含在我的项目中。我将两个头文件都导入了我的班级。 NSString 看起来实现正确,JSONValue 方法看起来不错: - (id)JSONValue { SBJsonParser *parser = [[SBJsonParser alloc] init]; id repr = [解析器 objectWithString:self]; if (!repr) NSLog(@"-JSONValue failed. Error is: %@", parser.error);返回代表; }
    • 不,它是 .m 文件。它没有被编译并链接到您的可执行文件。
    【解决方案3】:

    错误表示JSONValue方法未知。你在课堂上添加了#import "SBJSON.h" 吗?

    【讨论】:

      【解决方案4】:

      这里的问题是您没有将 SBJSON 正确导入到您的项目中。查看并确保在源文件顶部包含了带有正确标题的 import 语句。

      【讨论】:

        猜你喜欢
        • 2011-09-16
        • 2012-05-19
        • 1970-01-01
        • 1970-01-01
        • 2017-12-01
        • 1970-01-01
        • 2023-03-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多