1.将字典或者数组反序列化为NSString. NSMutableDictionary *jsonDic = [NSMutableDictionary dictionary]; NSMutableDictionary *alert = [NSMutableDictionary dictionary]; NSMutableDictionary *aps = [NSMutableDictionary dictionary]; [alert setObject:@"a msg come!" forKey:@"body"]; [aps setObject:alert forKey:@"alert"]; [aps setObject:@"3" forKey:@"bage" ]; [aps setObject:@"def.mp3" forKey:@"sound"]; [jsonDic setObject:aps forKey:@"aps"]; NSString *strJson = [jsonDic JSONString]; NSLog(@"%@",strJson); // 2.将NSString反序列化为数组、字典. NSDictionary *result = [strJson objectFromJSONString]; // 或者 NSDictionary *result = [dataJson objectFromJSONData]; NSLog(@"%@",result);
NSError *error;NSString *textFileContents = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"json" ofType:@"txt"] encoding:NSUTF8StringEncoding error:&error];if (textFileContents == nil) {NSLog(@"Error reading text file. %@", [error localizedFailureReason]);}NSDictionary *result = [textFileContents objectFromJSONString];NSLog(@"res= %@", [result objectForKey:@"cmd"]);
读取jsonKIT文件,转化成json字典
stirng 读取json文件 jsonkit 使用
最新推荐文章于 2024-06-04 14:45:00 发布