美文网首页
iOS中对象转为JSON字符串后,多出来反斜杠的问题

iOS中对象转为JSON字符串后,多出来反斜杠的问题

作者: 浩成哥哥 | 来源:发表于2018-12-19 16:49 被阅读0次

iOS中使用NSJSONSerialization把对象转为JSON字符串后,多出来反斜杠的问题

代码

NSDictionary *dic = @{@"url": @"http://..."};

NSLog(@"%@", dic);

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:0 error:nil];

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

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

NSLog(@"%@, json object = %@", jsonString, jsonDict);

执行结果:

2016-12-15 16:49:45.009 test[14989:546596] {

url = "http://...";

}

2016-12-15 16:49:45.010 test[14989:546596] {"url":"http://..."}, json object = {

url = "http://...";

}

转换后的json字符串中url地址被转义了 :(

使用字符串替换可以事后弥补:

[jsonString stringByReplacingOccurrencesOfString:@"\" withString:@""];

相关文章

网友评论

      本文标题:iOS中对象转为JSON字符串后,多出来反斜杠的问题

      本文链接:https://www.haomeiwen.com/subject/xxgjkqtx.html