1. 字典转变成json字符串:
+(NSString*)dictToJsonStr:(NSDictionary*)dict{
NSString*appendStr;
if([NSJSONSerialization isValidJSONObject:dict]) {
NSError*error;
NSData*registerData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
appendStr = [[NSString alloc]initWithData:registerData encoding:NSUTF8StringEncoding];
}
returnappendStr;
}
2.json字符串转变成字典
-(NSArray*)dictionaryWithJsonString:(NSString*)jsonString {
if(jsonString == nil) {
return nil;
}
NSData*jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError*err;
//解析出来的是数组或者是字典等类型,就是用相应的类型来接收
NSArray* array = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
if(err) {
NSLog(@"json解析失败:%@",err);
return nil;
}
NSLog(@"dic*********%@",array);
return array;
}
3.
4.
5.