gpt4 book ai didi

json - 解析为json字符串正确格式

转载 作者:行者123 更新时间:2023-11-30 12:36:09 28 4
gpt4 key购买 nike

我在字典中有一个字符串,我试图将其传递给 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\"}]}"}

最佳答案

我猜测这就是您看到的输出:

{
"_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 特殊字符进行转义(即 "\)。这就是您所要求的,并且编码正确。

如果您希望 body 成为 JSON 对象而不是字符串,则需要首先通过反序列化将其转换为 JSON,然后设置 body 键然后序列化整个消息。

关于json - 解析为json字符串正确格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42853379/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com