-1

[{\"destination\":\"Bhainsa\",\"travelMode\":\"Flight\",\"company\":\"Yes\",\"origin\":\"Adilabad\"}]

// This is MY Code

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

let jsonString = String(data: data1, encoding: .utf8)!
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
Vikash Gupta
  • 31
  • 1
  • 3
  • 2
    Are you sure there are backslashes?. Usually they are virtually added to escape double quotes in a literal string. – vadian Oct 15 '18 at 12:01
  • ▿ 0 : 4 elements ▿ 0 : 2 elements - key : destination - value : Anantapur ▿ 1 : 2 elements - key : travelMode - value : Flight ▿ 2 : 2 elements - key : company - value : Yes ▿ 3 : 2 elements - key : origin - value : Adilabad this is my Array and i converted this in String Formate . and i got this result . "[{\"destination\":\"Anantapur\",\"travelMode\":\"Flight\",\"company\":\"Yes\",\"origin\":\"Adilabad\"}]" but i don't want this Single slash . you know how to remove this Single slash \ – Vikash Gupta Oct 15 '18 at 12:12
  • 1
    Once again the backslashes are added when you `print` the string. There are no backslashes unless one of the dictionary values is a JSON string. Try it in Playground. The backslashes should **not** appear in the result sidebar. – vadian Oct 15 '18 at 12:28
  • 1
    What about testing what @vadian said? Just create a `UILabel` somewhere and set its `text` property to `jsonString`. Check if you see again the backslash. – Larme Oct 15 '18 at 12:41

1 Answers1

1

You may need

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"}]
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • it's came jsonobject formate but i want return in String formate – Vikash Gupta Oct 15 '18 at 12:00
  • and i can't make my string like this // """ [{\"destination\":\"Bhainsa\",\"travelMode\":\"Flight\",\"company\":\"Yes\",\"origin\":\"Adilabad\"}] """ because i made one array then converted in string formate but here is single slash come but i want to remove single slash and return in only string formate . i want return this Formate // "[{"destination":"Port Blair","travelMode":"Flight","company":"No","origin":"Adilabad"}]" – Vikash Gupta Oct 15 '18 at 12:04
  • let ggg = str.replacingOccurrences(of: "\\", with: "") . i already try this code but it's not workin. i got same result when i use this code . this is same result "[{\"destination\":\"Adilabad\",\"travelMode\":\"Flight\",\"company\":\"Yes\",\"origin\":\"Port Blair\"}]" – Vikash Gupta Oct 15 '18 at 12:13
  • i converted my Array in String and i got this string "[{\"destination\":\"Bhainsa\",\"travelMode\":\"Flight\",\"company\":\"Yes\",\"origin\":\"Adilabad\"}]" but i want to remove this Single slash also . and after remove i want in string formate not in jsonobject – Vikash Gupta Oct 15 '18 at 12:17