简体   繁体   English

无法访问Objective-C中的JSON数据

[英]unable to access JSON data in Objective-C

Ok, this is my first approach to JSONs in Objective-C (and i'm quite new to the last one too). 好的,这是我在Objective-C中使用JSON的第一种方法(而且我对最后一种方法也很陌生)。 i'm to get infos stored my json to use them in Objective-C, but when trying to load it i get null in response from NSLog(@"%@",allData); 我要让信息存储我的json以便在Objective-C中使用它们,但是当尝试加载它时,我从NSLog(@"%@",allData);得到响应为空NSLog(@"%@",allData); on. 上。 Can anybody please tell me what i am doing wrong? 有人可以告诉我我在做什么错吗? thanks in advance for your time and your patience. 预先感谢您的时间和耐心等待。 oh and if needed here's the json: http://jsonviewer.stack.hu/#http://conqui.it/ricette.json 哦,如果需要的话,这里是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);

The JSONObjectWithData method has an error parameter, of which you can avail yourself in order to diagnose the problem. JSONObjectWithData方法具有error参数,您可以利用该参数来诊断问题。 For example: 例如:

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

In your comments, you suggest that you received an error about "Unescaped control character around character 414." 在您的评论中,建议您收到有关“字符414附近的转义控制字符”的错误。 That would suggest an error in the JSON, itself, which you might want to validate by copying into http://jsonlint.com/ and see if it reports any issues. 这将提示JSON本身存在错误,您可能需要通过将其复制到http://jsonlint.com/并查看其是否报告任何问题来进行验证。

In response to the broader question about whether there are any Objective-C issues, there are no coding errors, per se. 在回答有关是否存在任何Objective-C问题的更广泛的问题时, 本质上没有编码错误 I can't comment on the for loop which clearly assumes that allData is an array of dictionaries to which I cannot attest without seeing the JSON. 我无法对for循环发表评论,因为该循环显然假定allData是字典数组,如果不查看JSON就无法向其证明。 But I'll take your word for it. 但我会相信你的。 But, yes, the Objective-C code looks fine (albeit, a little light on checking of the return values types and error objects). 但是,是的,Objective-C代码看起来不错(尽管稍微检查了返回值类型和错误对象)。

For example, if you wanted some diagnostic assert statements that you could use during development, you might do something like: 例如,如果您想在开发期间使用某些诊断断言语句,则可以执行以下操作:

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

If any of these errors were possible runtime errors in production, you'd replace assert statements with if statements that do the necessary error handling. 如果这些错误中的任何一个可能是生产中的运行时错误,则可以用执行必要错误处理的if语句替换assert语句。 But hopefully it illustrates the concept. 但希望它能说明这个概念。

Problem in your response is that , string values are unable to concatenate.So, I have to manually remove those tabs and new lines. 您的响应中的问题是,字符串值无法连接。因此,我必须手动删除这些制表符和换行。

At five places you are getting error ie: 在五个地方,您会遇到错误,即:

pelate. pelate。

pasta. 意大利面条。

pomodoro. 番茄钟。

saporiti). saporiti)。

\\t \\ 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.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM