iOS 实际项目出现的问题

iOS开发--字典(NSDictionary)和JSON字符串(

2018-09-19  本文已影响7人  江河_ios
  1. 字典转Json字符串

// 字典转json字符串方法

-(NSString *)convertToJsonData:(NSDictionary *)dict

{

NSError *error;

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];

NSString *jsonString;

if (!jsonData) {

    NSLog(@"%@",error);

}else{

    jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];

}

NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];

NSRange range = {0,jsonString.length};

//去掉字符串中的空格

[mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];

NSRange range2 = {0,mutStr.length};

//去掉字符串中的换行符

[mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];

return mutStr;

}

  1. JSON字符串转化为字典
上一篇 下一篇

猜你喜欢

热点阅读