iOS 自带的解析json的类。
1。数据源是NSData类型的,也可以是NSString类型的,但是NSString类型的可以转换为NSData类型的.
NSString *responseString;
NSData *da= [responseString dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
if ([jsonObject isKindOfClass:[NSDictionary class]]){
NSDictionary *dictionary = (NSDictionary *)data;
NSLog(@"Dersialized JSON Dictionary = %@", deserializedDictionary);
}else if ([jsonObject isKindOfClass:[NSArray class]]){
NSArray *nsArray = (NSArray *)jsonObject;
NSLog(@"Dersialized JSON Array = %@", deserializedArray);
} else {
NSLog(@"An error happened while deserializing the JSON data.");
}
2,读取项目下的文件代码。
NSString *mainBundleDirectory=[[NSBundle mainBundle] bundlePath];
NSString *path=[mainBundleDirectory stringByAppendingPathComponent:@"123.txt"];
NSURL *url=[NSURL fileURLWithPath:path];
NSData *data = [[NSDataalloc] initWithContentsOfURL:url];
NSString* aStr=[[NSStringalloc] initWithData:data encoding:NSUTF8StringEncoding];