2

好的,这是我在 Objective-C 中使用 JSON 的第一种方法(我对最后一种方法也很陌生)。我要获取存储在我的 json 中的信息以在 Objective-C 中使用它们,但是当尝试加载它时,我得到 null 作为响应NSLog(@"%@",allData);。谁能告诉我我做错了什么?提前感谢您的时间和耐心。哦,如果需要,这里是 json: http://jsonviewer.stack.hu/#http: //conqui.it/ricette.json

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"recipes" ofType:@"json"];

NSError *error = nil;

NSMutableData *JSONData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:&error];
NSLog(@"%@",JSONData);


NSArray *allData = [NSJSONSerialization JSONObjectWithData:JSONData options:0 error:nil];
NSLog(@"%@",allData);

for (NSDictionary *diction in allData) {
    NSString *recipe = [diction objectForKey:@"recipe"];

    [array addObject:recipe];
}

NSLog(@"%@",array);
4

2 回答 2

4

JSONObjectWithData方法有一个error参数,您可以利用该参数来诊断问题。例如:

NSError *error = nil;
NSArray *allData = [NSJSONSerialization JSONObjectWithData:JSONData
                                                   options:0
                                                     error:&error];
if (error)
    NSLog(@"%s: JSONObjectWithData error: %@", __FUNCTION__, error);

在您的评论中,您建议您收到有关“字符 414 周围未转义的控制字符”的错误。这表明 JSON 本身存在错误,您可能希望通过复制到http://jsonlint.com/来验证它并查看它是否报告任何问题。

为了回答关于是否存在任何 Objective-C 问题的更广泛的问题,本身没有编码错误。我无法评论for循环,它清楚地假设 allData 是一个字典数组,如果没有看到 JSON,我就无法证明它。但我会相信你的话。但是,是的,Objective-C 代码看起来不错(尽管对返回值类型和错误对象的检查有点轻率)。

例如,如果您想要一些可以在开发期间使用的诊断断言语句,您可以执行以下操作:

NSArray *allData = [NSJSONSerialization JSONObjectWithData:JSONData options:0 error:nil];
NSAssert(error, @"%s: JSONObjectWithData error: %@", __FUNCTION__, error);

NSLog(@"%s: array=%@", __FUNCTION__, array);

NSAssert([allData isKindOfClass:[NSArray class]], @"allData is not an array");

for (NSDictionary *diction in allData) {
    NSAssert([diction isKindOfClass:[NSDictionary class]], @"%s: diction is not a dictionary (%@), __FUNCTION__, diction);

    NSString *recipe = [diction objectForKey:@"recipe"];

    NSAssert(recipe, @"%s: Did not find recipe key in diction (%@)", __FUNCTION__, diction);

    [array addObject:recipe];
}

如果这些错误中的任何一个是生产中可能的运行时错误,您可以将 assert 语句替换if为执行必要错误处理的语句。但希望它能说明这个概念。

于 2013-07-07T21:30:42.873 回答
0

您的回复中的问题是,字符串值无法连接。因此,我必须手动删除这些选项卡和新行。

在五个地方你会得到错误,即:

果皮。

意大利面。

番茄钟。

精神分裂症)。

\t

- (void)viewDidLoad
{
    NSString *urlStr=[NSString stringWithFormat:@"http://www.conqui.it/ricette.json"];

    urlStr=[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *fileNameURL=[NSURL URLWithString:urlStr];

    NSLog(@"url is %@",urlStr);

    NSMutableURLRequest *filenameReq=[[NSMutableURLRequest alloc] initWithURL:fileNameURL];
    NSData *responseData=[NSURLConnection sendSynchronousRequest:filenameReq returningResponse:nil error:nil];

    NSString *responseString=[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

    responseString=[[responseString componentsSeparatedByString:@"\n"] componentsJoinedByString:@""];
    responseString=[responseString stringByReplacingOccurrencesOfString:@"\t" withString:@""];

    responseData=[responseString dataUsingEncoding:NSUTF8StringEncoding];

    NSLog(@"response String is %@",responseString);

    [NSCharacterSet characterSetWithCharactersInString:responseString];

    NSError *e = nil;
    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: 0 error: &e];

    NSLog(@"JSON Array is %@ & error is %@",jsonArray,e);

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
于 2013-07-08T07:28:28.887 回答