iOS objective-c – JSONstring到NSDictionaryexception

我对Objective-C比较陌生,并且在MapKit(位于这里 )的介绍中学习了一个教程。 我一直在试图debugging传递给NSDictionary的JSONstring的问题。 我收到以下错误:

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的string有关。 这是创buildJSONstring的代码:

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

string正确填充,并在terminal显示是这样的(整个事情是巨大的,所以我只会发一小笔):

 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,所以我不确定问题是什么。

该问题是由于尝试将消息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应通过使用一个类别dynamic地将JSONValue方法添加到NSString类。 在这里解释

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

其实问题不是缺less标题导入(嗯,这可能是问题的一部分),但缺less实施.m文件。 您需要确保NSObject+SBJson.m包含在您的项目中,可以单独使用,也可以作为库的一部分。

该错误表明JSONValue方法未知。 你有没有把#import "SBJSON.h"join你的课堂?

这里的问题是,您没有正确导入SBJSON到您的项目。 看一看,并确保你已经在源文件的顶部包含正确的标题的导入语句。