gpt4 book ai didi

ios - 从转换后的 json 字符串中删除\n 和斜杠\

转载 作者:可可西里 更新时间:2023-11-01 00:55:11 26 4
gpt4 key购买 nike

通过以下行,我将 dict 转换为 json 字符串:

 let dummyCom = ["companyId" : company.getCompanyId()?.stringValue]

var error : NSError?

let jsonData = try! JSONSerialization.data(withJSONObject: dummyCom, options: JSONSerialization.WritingOptions.prettyPrinted)

var jsonString = String(data: jsonData, encoding: String.Encoding.utf8) // the data will be converted to the string

我得到以下描述:jsonString的打印说明:

▿ Optional<String>
- some : "{\n \"companyId\" : \"1\"\n}"

我的问题是如何从字符串中删除\n 和\。

我试过这个: jsonString = jsonString!.removingPercentEncoding但得到相同的结果。任何帮助或建议都会有帮助

最佳答案

只需将 JSONSerialization.WritingOptions.prettyPrinted 替换为 []

你会得到:

"{"companyId":1}"

您的代码将如下所示:

let dummyCom = ["companyId" : 1]

var error : NSError?

let jsonData = try! JSONSerialization.data(withJSONObject: dummyCom, options: [])

var jsonString = String(data: jsonData, encoding: String.Encoding.utf8)

关于ios - 从转换后的 json 字符串中删除\n 和斜杠\,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51221436/

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