2

わかりました、これは Objective-C での JSON への私の最初のアプローチです (そして、私は最後のものにもまったく慣れていません)。私はObjective-Cでそれらを使用するために私のjsonに保存された情報を取得しますが、それを読み込もうとすると、NSLog(@"%@",allData);上からの応答でnullが返されます。誰が私が間違っているのか教えてもらえますか? あなたの時間とあなたの忍耐に前もって感謝します. ああ、必要に応じて、ここに 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 の問題があるかどうかについてのより広範な質問への回答として、コーディング エラー自体はありません。forallData が、JSON を見ないと証明できない辞書の配列であると明確に想定しているループについてコメントすることはできません。しかし、私はあなたの言葉を受け入れます。しかし、はい、Objective-C コードは問題ないように見えます (ただし、戻り値の型とエラー オブジェクトのチェックについては少しわかりにくいですが)。

たとえば、開発中に使用できる診断 assert ステートメントが必要な場合は、次のようにします。

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

あなたの応答の問題は、文字列値を連結できないことです。そのため、これらのタブと改行を手動で削除する必要があります。

次の 5 つの場所でエラーが発生しています。

毛皮。

パスタ。

ポモドーロ。

サポリティ)。

\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 に答える