标签:objective-c json的解析 乱码 文件操作
一、什么是Json?/* Returns YES if the given object can be converted to JSON data, NO otherwise. The object must have the following properties: - Top level object is an NSArray or NSDictionary - All objects are NSString, NSNumber, NSArray, NSDictionary, or NSNull - All dictionary keys are NSStrings - NSNumbers are not NaN or infinity Other rules may apply. Calling this method or attempting a conversion are the definitive ways to tell if a given object can be converted to JSON data. */ + (BOOL)isValidJSONObject:(id)obj;
NSURL * url = [NSURL URLWithString:PATH];//把字符串类型是路径转换为NSURL 类型的 NSData * data = [NSData dataWithContentsOfURL:url];/向从URL中拿数据, NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];//用一个字典接收返回来的数据。
/* Create a Foundation object from JSON data. Set the NSJSONReadingAllowFragments option if the parser should allow top-level objects that are not an NSArray or NSDictionary. Setting the NSJSONReadingMutableContainers option will make the parser generate mutable NSArrays and NSDictionaries. Setting the NSJSONReadingMutableLeaves option will make the parser generate mutable NSString objects. If an error occurs during the parse, then the error parameter will be set and the result will be nil. The data must be in one of the 5 supported encodings listed in the JSON specification: UTF-8, UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE. The data may or may not have a BOM. The most efficient encoding to use for parsing is UTF-8, so if you have a choice in encoding the data passed to this method, use UTF-8. */ + (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error;剩下的工作就不难完成了。就是怎么从返回的字典中拿我们需要的数据了。这些就和Json的解析没多大关系了。只要理清层级关系,一点一点的拿就行了。
@property (nonatomic,copy) NSString * ARTIST; //艺术家 @property (nonatomic,copy) NSString * COMPANY; //公司 @property (nonatomic,copy) NSString * COUNTRY; //国家 @property (nonatomic,copy) NSString * PRICE; //价格 @property (nonatomic,copy) NSString * TITLE; //专辑名 @property (nonatomic,copy) NSString * YEAR; //年代我们再在解析这个Json类里面声明一个可变数组- (NSArray *)preaseJson;
- (NSArray *)preaseJson{ /****************************JSON的解析*************************************/ NSURL * url = [NSURL URLWithString:PATH]; NSData * data = [NSData dataWithContentsOfURL:url]; NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; NSArray * cdArr = dic[@"CATALOG"][@"CD"]; //遍历CD对应的数组 for (NSDictionary * di in cdArr) { DataModel * mo = [[DataModel alloc] init]; // mo.ARTIST = di[@"ARTIST"]; // mo.COMPANY = di[@"COMPANY"]; // mo.COUNTRY = di[@"COUNTRY"]; // mo.PRICE = di[@"PRICE"]; // mo.TITLE = di[@"TITLE"]; // mo.YEAR = di[@"YEAR"]; // [_arr addObject:mo]; // KVC (键 --- 值编码) //这种方法和上面注释的方法二选一,使用这种方法就需要在数据模型里面实现一个打印的方法(这个打印方法可没有),且上面声明的成员的名字必须和字典里面的key名一样。 [mo setValuesForKeysWithDictionary:di]; [_arr addObject:mo]; } return _arr; }
- (void)print{ MyJson * myjson = [[MyJson alloc] init]; NSArray * array = [myjson preaseJson]; int i = 0; for (DataModel * dm in array) { printf("------------- %d -------------\n",++i); printf(" ARTIST : %s\n",[[dm ARTIST] UTF8String]); //艺术家 printf(" COMPANY : %s\n",[[dm COMPANY] UTF8String]);//唱片公司 printf(" COUNTRY : %s\n",[[dm COUNTRY] UTF8String]);//国家 printf(" PRICE : %s\n",[[dm PRICE] UTF8String]); //价格 printf(" TITLE : %s\n",[[dm TITLE] UTF8String]); //专辑名 printf(" YEAR : %s\n",[[dm YEAR] UTF8String]); //年代 // [dm show]; //这个show方法也是把上面的6行printf封装一下 } } + (void)test{ MyJson * js = [[MyJson alloc] init]; [js print]; }最后在main函数里面就只有一条语句:[MyJson test];
+ (void)dataPacket{ MyJson * json = [[MyJson alloc] init]; NSArray * dataArr = [json preaseJson]; // 数组dataArr中存储 解析后的数据模型 NSMutableString * str = [[NSMutableString alloc] init];//新建一个可变字符串 NSFileManager * myFile = [NSFileManager defaultManager]; int i = 0; for (DataModel * md in dataArr) { [str appendFormat:@"%d %@",++i,@"-----------------------------\n"]; [str appendFormat:@"%@%@%@",@" ARTIST : ",md.ARTIST,@"\n"]; [str appendFormat:@"%@%@%@",@" COMPANY : ",md.COMPANY,@"\n"]; [str appendFormat:@"%@%@%@",@" COUNTRY : ",md.COUNTRY,@"\n"]; [str appendFormat:@"%@%@%@",@" PRICE : ",md.PRICE,@"\n"]; [str appendFormat:@"%@%@%@",@" TITLE : ",md.TITLE,@"\n"]; [str appendFormat:@"%@%@%@",@" YEAR : ",md.YEAR,@"\n"]; } NSData * data = [str dataUsingEncoding:NSUTF8StringEncoding]; [myFile createFileAtPath:SAVEPATH contents:data attributes:nil]; }最后几句中的SAVEPATH就是存储路径,需要带上文件格式的,就像你看一个文件的属性里面显示的那样,如:/user/Desktop/123.txt。
关于KVC 键值编码还不是很会使用,以后熟练了再写出来。
最后上传一张图片关于使用苹果自带浏览器打开json文档乱码的问题是怎么解决的。
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:objective-c json的解析 乱码 文件操作
原文地址:http://blog.csdn.net/zhuming3834/article/details/47285237