0

For example:

let array = ["1234","5678"]

this array of values should be converted to the following string -

"[\"1234\",\"5678\"]"

Although I found answer here - Objective-C - How to convert NSString to escaped JSON string? but it doesn't look clean. Is there some cleaner way to do this ?

I am able to get this output - "["one","two"]" using

NSJSONSerialization.dataWithJSONObject

but how to have backslashes in between of strings?

3
  • @Eric Aya I am able to convert my array into strings i.e this output - "["one","two"]" , but how to add escaped characters ? Commented Feb 3, 2017 at 15:06
  • 1
    In the string ["one","two"], the "'s are characters which have been escaped (that's why they appear in the string itself, rather than delimiting separate strings). Try doing print("[\"1234\",\"5678\"]") – you'll get an output of ["one","two"]. Now try doing debugPrint("[\"1234\",\"5678\"]") – Swift will insert backslashes into the output to show that the " characters are part of the actual string. You don't need to "add escaped characters". Commented Feb 3, 2017 at 15:18
  • 1
    @AjayKumar This is a common mistake: you don't need to do that unless your server is badly configured - and then you would have to fix the server, not to circumvent the issue from client side. // And don't forget that anyway you should send JSON data to a server, not a String in JSON format. Commented Feb 3, 2017 at 15:22

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.