我在字典中有一个字符串,我试图传递给jsonstring,但它没有删除反斜杠,服务器也没有吞下它。有什么想法吗?
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\"}]}"
编辑:
if let json = try? JSONSerialization.data(withJSONObject: dataToPost, options: []) {
if let contentJSONString = String(data: json, encoding: String.Encoding.utf8) {
// here `content` is the JSON dictionary containing the String
dataPost.append(contentJSONString)
print(contentJSONString)
}
}
这会返回我的字符串但不会消失反斜杠。
JSON string = {"_Abstract":"Problema al instalar Historian 6.0 Enterprise Info Link: https:\/\/soporte.adasoft.es\/browse\/NOVTRACERT-24","Executor":"","Watcher":[""],"Supervisor":"César Ramos","Name":"NOVTRACERT-24","Body":"{\"type\": \"multipart\",\"content\": [{\"contentType\": \"multipart\/alternative; Boundary=\\\"0__=4EBB0A76DFCD51048f9e8a93df938690918c4EBB0A76DFCD5104\\\"\",\"contentDisposition\": \"inline\" },{\"contentType\": \"text\/plain; charset=US-ASCII\",\"data\": \"Problema al instalar Historian 6.0 Enterprise\r\n\r\nInfo Link: https:\/\/soporte.adasoft.es\/browse\/NOVTRACERT-24\",\"boundary\": \"--0__=4EBB0A76DFCD51048f9e8a93df938690918c4EBB0A76DFCD5104\" }, {\"contentType\": \"text\/html; charset=US-ASCII\",\"contentDisposition\": \"inline\",\"data\": \"<html><body>Problema al instalar Historian 6.0 Enterprise\r\n\r\nInfo Link: https:\/\/soporte.adasoft.es\/browse\/NOVTRACERT-24<\/body><\/html>\",\"boundary\": \"--0__=4EBB0A76DFCD51048f9e8a93df938690918c4EBB0A76DFCD5104\"}]}"}
答案 0 :(得分:0)
我要猜测这是你看到的输出:
{
"_Abstract": "Problema al instalar Historian 6.0 Enterprise Info Link: https:\/\/soporte.adasoft.es\/browse\/NOVTRACERT-24",
"Executor": "",
"Watcher": [""],
"Supervisor": "César Ramos",
"Name": "NOVTRACERT-24",
"Body": "{\"type\": \"multipart\",\"content\": [{\"contentType\": \"multipart\/alternative; Boundary=\\\"0__=4EBB0A76DFCD51048f9e8a93df938690918c4EBB0A76DFCD5104\\\"\",\"contentDisposition\": \"inline\" },{\"contentType\": \"text\/plain; charset=US-ASCII\",\"data\": \"Problema al instalar Historian 6.0 Enterprise\r\n\r\nInfo Link: https:\/\/soporte.adasoft.es\/browse\/NOVTRACERT-24\",\"boundary\": \"--0__=4EBB0A76DFCD51048f9e8a93df938690918c4EBB0A76DFCD5104\" }, {\"contentType\": \"text\/html; charset=US-ASCII\",\"contentDisposition\": \"inline\",\"data\": \"<html><body>Problema al instalar Historian 6.0 Enterprise\r\n\r\nInfo Link: https:\/\/soporte.adasoft.es\/browse\/NOVTRACERT-24<\/body><\/html>\",\"boundary\": \"--0__=4EBB0A76DFCD51048f9e8a93df938690918c4EBB0A76DFCD5104\"}]}"
}
我通过JSON Lint运行它来美化它并检查它是否是有效的JSON。 body
项是一个包含JSON的字符串(在本例中),但由于它在另一段JSON中被序列化为字符串,因此需要对所有JSON特殊字符进行转义(即{{1}和"
)。这就是您要求的,并且编码正确。
如果您希望\
成为JSON对象而不是字符串,则需要先通过反序列化将其转换为JSON,然后设置body
密钥,然后序列化整个消息