iOSデータ解析のJSON解析
4257 ワード
{
"reason": "success",
"result": [
{
"movieId": "215977",
"movieName": " ",
"pic_url": "http://v.juhe.cn/movie/picurl?2583247"
},
{
"movieId": "215874",
"movieName": " , ",
"pic_url": "http://v.juhe.cn/movie/picurl?2583542"
},
{
"movieId": "215823",
"movieName": " ",
"pic_url": "http://v.juhe.cn/movie/picurl?2583092"
}
],
"error_code": 0
}
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);
}
}
}
}