我有一个可能包含引号、\、/、\r、\n 的 NSString,我想将它转换为 JSON 编码的字符串,所以像这样的字符串
“文本1\文本2”
变成
\"文本1\\文本2\"
是否有现有功能可以让我这样做?
另外,我在我的项目中使用 SBJson,但我找不到 SBJson 是否可以这样做。
NSJSONSerialization 不在桌面上,因为我的应用程序仍然需要支持 OSX 10.6
我有一个可能包含引号、\、/、\r、\n 的 NSString,我想将它转换为 JSON 编码的字符串,所以像这样的字符串
“文本1\文本2”
变成
\"文本1\\文本2\"
是否有现有功能可以让我这样做?
另外,我在我的项目中使用 SBJson,但我找不到 SBJson 是否可以这样做。
NSJSONSerialization 不在桌面上,因为我的应用程序仍然需要支持 OSX 10.6
这回答了你的问题了吗?
-(NSString *)JSONString:(NSString *)aString {
NSMutableString *s = [NSMutableString stringWithString:aString];
[s replaceOccurrencesOfString:@"\\" withString:@"\\\\" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
[s replaceOccurrencesOfString:@"\"" withString:@"\\\"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
[s replaceOccurrencesOfString:@"/" withString:@"\\/" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
[s replaceOccurrencesOfString:@"\n" withString:@"\\n" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
[s replaceOccurrencesOfString:@"\b" withString:@"\\b" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
[s replaceOccurrencesOfString:@"\f" withString:@"\\f" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
[s replaceOccurrencesOfString:@"\r" withString:@"\\r" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
[s replaceOccurrencesOfString:@"\t" withString:@"\\t" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
return [NSString stringWithString:s];
}
让我们以更原生的 JSON 方式来做吧;)
如果您喜欢我的回答,请在https://github.com/wanjochan给我加注星标
//trick: id(@[s]) => string => trim [] => target
//NSString *s
s=[[NSString alloc] initWithData:
[NSJSONSerialization dataWithJSONObject:@[s] options:0 error:nil]
encoding:NSUTF8StringEncoding];
s=[[s substringToIndex:([s length]-1)] substringFromIndex:1];
斯威夫特 2:
/// Escape reserved characters to produce a valid JSON String
/// For example double quotes `"` are replaced by `\"`
/// - parameters:
/// - String: an unescaped string
/// - returns: valid escaped JSON string
func JSONString(str: String?) -> String? {
var result : String? = nil
if let str = str {
result = str
.stringByReplacingOccurrencesOfString("\"", withString: "\\\"", options: .CaseInsensitiveSearch)
.stringByReplacingOccurrencesOfString("/", withString: "\\/", options: .CaseInsensitiveSearch)
.stringByReplacingOccurrencesOfString("\n", withString: "\\n", options: .CaseInsensitiveSearch)
.stringByReplacingOccurrencesOfString("\u{8}", withString: "\\b", options: .CaseInsensitiveSearch)
.stringByReplacingOccurrencesOfString("\u{12}", withString: "\\f", options: .CaseInsensitiveSearch)
.stringByReplacingOccurrencesOfString("\r", withString: "\\r", options: .CaseInsensitiveSearch)
.stringByReplacingOccurrencesOfString("\t", withString: "\\t", options: .CaseInsensitiveSearch)
}
return result
}
斯威夫特 3:
func JSONString(str: String) -> String {
var result = str
result = result.replacingOccurrences(of: "\"", with: "\\\"")
.replacingOccurrences(of: "/", with: "\\/")
.replacingOccurrences(of: "\n", with: "\\n")
.replacingOccurrences(of: "\u{8}", with: "\\b")
.replacingOccurrences(of: "\u{12}", with: "\\f")
.replacingOccurrences(of: "\r", with: "\\r")
.replacingOccurrences(of: "\t", with: "\\t")
return result
}
对防止错误非常有用:Domain=NSCocoaErrorDomain Code=3840 "Invalid escape sequence around character 123."
斯威夫特 4 解决方案
extension String {
var stringEncodedJSON: String {
var copy = self
let encodingDict: [String: String] = ["\"": "\\\"", "/": "\\/", "\n": "\\n", "\u{8}": "\\b","\u{12}": "\\f", "\r": "\\r", "\t": "\\t"]
encodingDict.forEach({ copy = copy.replacingOccurrences(of: $0, with: $1) })
return copy
}
}