iOS Objective-C-JSON字符串转换为NSDictionary异常 [英] iOS objective-c - JSON String to NSDictionary Exception

查看:354
本文介绍了iOS Objective-C-JSON字符串转换为NSDictionary异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Objective-C较陌生,并遵循了有关MapKit简介的教程(位于

I'm relatively new to Objective-C, and followed a tutorial on an introduction to MapKit (located here). I've been trying to debug an issue with a JSON string being passed to an NSDictionary. I'm getting the following error:

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

我将其范围缩小到出现问题的那一行:

I've narrowed it down to this line that's giving me the issue:

NSDictionary * root = [responseString JSONValue];

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

According to the SBJSON library documentation, this is possible. I'm thinking it has something to do with the string that I'm passing to the NSDictionary. Here is the code that creates the JSON string:

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

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

The string is getting populated correctly, and in the terminal it is showing like this (The whole thing is huge, so I'll only post a small amount):

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

Any help would be appreciated. I realize the tutorial I'm using is a little outdated, but I've never worked with JSON before, so I'm not really sure what the issue is.

推荐答案

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

The issue is caused by trying to send the message JSONValue to an instance in NSString, which doesn't support this method. try the following code instead

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

这应该为您提供一个包含响应数据的NSDictionaries NSArray.

This should give you an NSArray of NSDictionaries containing the response data.

您也可以在此处尝试复制和粘贴整个JSON响应以确认其有效

You can also try copying and pasting your entire JSON response here to check it valid

http://jsonformatter.curiousconcept.com/

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

Although as some of the other answers point out, #importing SBJSON.h should add the JSONValue method to NSString class dynamically by using a category. Explained here

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

这篇关于iOS Objective-C-JSON字符串转换为NSDictionary异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆