Remove against bars for json string format

0

I have a string inside a dictionary that I am trying to pass to jsonstring but it is not removing the backbars and the server does not swallow it.

Any ideas? The code is as follows:

let myBod = "\"Body\": {" +
        "\"type\": \"multipart\"," +
        "\"content\": [" +
        "{" +
        "\"contentType\": \"multipart/alternative; Boundary=\\"0__=4EBB0A76DFCD51048f9e8a93df938690918c4EBB0A76DFCD5104\\"\"," +
        "\"contentDisposition\": \"inline\"" +
         " }," +
         "{" +
         "\"contentType\": \"text/plain; charset=US-ASCII\"," +
        "\"data\": \"yappy\"," +
         "\"boundary\": \"--0__=4EBB0A76DFCD51048f9e8a93df938690918c4EBB0A76DFCD5104\"" +
         " }," +
        " {" +
        "\"contentType\": \"text/html; charset=US-ASCII\"," +
          "\"contentDisposition\": \"inline\"," +
         "\"data\": \"<html><body>yappy</body></html>\"," +
         "\"boundary\": \"--0__=4EBB0A76DFCD51048f9e8a93df938690918c4EBB0A76DFCD5104\"}]}"
    
asked by Alejandro 17.03.2017 в 10:13
source

1 answer

0

EDITED

The problem is in the String.

The bars are necessary to distinguish the quotes (") as a character part of the string.

In order for you to convert that string to JSON, it must first be a valid JSON. For this you can try it on this page jsonViewerOnline

What I have done for your case, is to add opening and closing keys to the String so that it can recognize it as a valid JSON.

I leave the following code that makes the String in JSON without problems

    var str = "\"Body\": {" +
               "\"type\": \"multipart\"," +
               "\"content\": [" +
                "{" +
             "\"contentType\": \"multipart/alternative; Boundary=\\"0__=4EBB0A76DFCD51048f9e8a93df938690918c4EBB0A76DFCD5104\\"\"," +
            "\"contentDisposition\": \"inline\"" +
             " }," +
              "{" +
        "\"contentType\": \"text/plain; charset=US-ASCII\"," +
        "\"data\": \"yappy\"," +
        "\"boundary\": \"--0__=4EBB0A76DFCD51048f9e8a93df938690918c4EBB0A76DFCD5104\"" +
        " }," +
        " {" +
        "\"contentType\": \"text/html; charset=US-ASCII\"," +
        "\"contentDisposition\": \"inline\"," +
        "\"data\": \"<html><body>yappy</body></html>\"," +
        "\"boundary\": \"--0__=4EBB0A76DFCD51048f9e8a93df938690918c4EBB0A76DFCD5104\"}]}"

       print(str + "\n")
       var newString = "{" + str + "}"
       print(newString + "\n")
       let data = newString.data(using: .utf8)
       let object = try!JSONSerialization.jsonObject(with: data!, options: .allowFragments)
       print(object)

You can perform tests on the XCode PlayGround.

    
answered by 22.03.2017 / 18:30
source