iOSネットワークデータ解析のJSON解析
FoundationによるJSON解析
ステップ1:JSONファイルパスの取得ステップ2:NSDataタイプへの変換ステップ3:JSONデータコードの解析:
-
(void)jsonParser {
//step1:
NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"MovieList" ofType:@"txt"];
//step2: NSData
NSData *jsonData = [NSData dataWithContentsOfFile:jsonPath];
//step3. json
NSError *error;
// :
//NSJSONReadingMutableContainers = (1UL << 0), 。
//NSJSONReadingMutableLeaves = (1UL << 1), NSMutableString, iOS7 。
//NSJSONReadingAllowFragments = (1UL << 2) json , json , json 。
NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error];
if (resultDic) {//
// json
if([NSJSONSerialization isValidJSONObject:resultDic]){
// json
NSData *strData = [NSJSONSerialization dataWithJSONObject:resultDic options:NSJSONWritingPrettyPrinted error:&error];
// strData
if (strData) {
// data
NSString *str = [[NSString alloc] initWithData:strData encoding:NSUTF8StringEncoding];
NSLog(@"%@",str);
}
}
}
}