Json字符串与字典互转

时间:2022-02-24 07:39:27

#pragma mark 转换json字符串

+(NSString *)toJSON:(id)aParam

{

NSData   *jsonData=[NSJSONSerialization dataWithJSONObject:aParam options:NSJSONWritingPrettyPrinted error:nil];

NSString *jsonStr=[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

return jsonStr;

}

#pragma mark 字符串转字典

+(NSDictionary *)dicWithJSonStr:(NSString *)jsonString

{

NSData * jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];

return dic;

}