这是我的字符串,我想从字符串中删除一些字符。我的代码在
之下"{ \"userpass\" : \"\", \"apikey\" : \"=\", \"deviceid\" : \"\", \"username\" : \"\"}"
我想转换成这种格式。
{
"userpass" : "",
"username" : "",
"deviceid" : "",
"apikey" : "="
}
如何从字符串中删除\
以使JSON字符串正确?
答案 0 :(得分:1)
如果你想从中获取字典,那么这是字符串响应中的json
试试这个,这里str
是你的字符串
let str = "{ \"userpass\" : \"\", \"apikey\" : \"=\", \"deviceid\" : \"\", \"username\" : \"\"}"
let data = str.dataUsingEncoding(NSUTF8StringEncoding)
do {
let dic = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as! NSDictionary
print(dic)
}
catch let e as NSError {
print(e.localizedDescription)
}
答案 1 :(得分:0)
如果你在调试监视器po myString
中看到它将是{ "userpass" : "", "apikey" : "=", "deviceid" : "", "username" : ""}
,这个斜杠只是在代码中没有在程序中
答案 2 :(得分:-1)
在objective-c
NSString *stringWithoutSpaces = [yourString
stringByReplacingOccurrencesOfString:@" " withString:@""];
答案 3 :(得分:-1)
All special characters with in string must use extra \ (escape sequence) to recognize the actual value
[string stringByReplacingOccurrencesOfString:@"\\n" withString:@""];
or string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
hope you will be clear