iOS json 字典互相转换

//字典转json格式字符串:

- (NSString*)dictionaryToJson:(NSDictionary*)dic

{NSError*parseError =nil;NSData*jsonData = [NSJSONSerializationdataWithJSONObject:dic options:NSJSONWritingPrettyPrintederror:&parseError];return[[NSStringalloc] initWithData:jsonData encoding:NSUTF8StringEncoding];  }

//json格式字符串转字典:

- (NSDictionary*)dictionaryWithJsonString:(NSString*)jsonString 

{if(jsonString ==nil) {returnnil;    }NSData*jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];NSError*err;NSDictionary*dic = [NSJSONSerializationJSONObjectWithData:jsonData                                                        options:NSJSONReadingMutableContainerserror:&err];if(err) {NSLog(@"json解析失败:%@",err);returnnil;    }return dic

你可能感兴趣的:(iOS json 字典互相转换)