Attention! Translated article might be found on my English blog.

2016年7月21日木曜日

Obj-CでJSONをNSArrayやNSDictionaryとして読み込む

-[NSJSONSerialization JSONObjectWithData:options:error:]を使う。

参考: iOSでjson | Professional Programmer

例えば

NSData *json = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"hoge" ofType:@"json"]];
    
id dic = [NSJSONSerialization JSONObjectWithData:json
                                             options:0
                                               error:nil];

返り値はNSArrayかNSDictionaryになるようなので、中身が不定なJSONの場合は
クラスをチェックする必要がありそうです。