//json格式字符串转字典:
- (NSDictionary*)dictionaryWithJsonString:(id)jsonString {
if (jsonString ==nil) {
returnnil;
}
NSError*err;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonString
options:NSJSONReadingMutableContainers
error:&err];
if(err) {
NSLog(@"json解析失败:%@",err);
returnnil;
}
return dic;
}
#pragma mark 字典转化字符串
-(NSString*)dictionaryToJson:(NSDictionary*)dic
{
NSError*parseError =nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
网友评论