从转换后的json字符串中删除斜杠\

时间:2018-10-15 11:39:34

标签: ios swift4

  

[{\“目的地\”:\“ Bhainsa \”,\“ travelMode \”:\“航班\”,\“公司\”:\“是\”,\“来源\”:\“阿迪拉巴德\“}]

//这是我的代码

let data1 = try! JSONSerialization.data(withJSONObject: arrat2, options: [])

let jsonString = String(data: data1, encoding: .utf8)!

1 个答案:

答案 0 :(得分:1)

您可能需要

let str = """
[{\"destination\":\"Bhainsa\",\"travelMode\":\"Flight\",\"company\":\"Yes\",\"origin\":\"Adilabad\"}]
"""  
let res = try! JSONSerialization.jsonObject(with: str.data(using:.utf8)!, options: []) as! [[String:Any]] 
let resStr = "\(res)"
print(resStr) // [["origin": Adilabad, "travelMode": Flight, "company": Yes, "destination": Bhainsa]]

let ggg = str.replacingOccurrences(of: "\\", with: "")
print(ggg) // [{"destination":"Bhainsa","travelMode":"Flight","company":"Yes","origin":"Adilabad"}]