gpt4 book ai didi

ios - 如何在 iOS Swift 3 中将 JSON 字符串转换为字典数组

转载 作者:行者123 更新时间:2023-11-28 11:00:04 24 4
gpt4 key购买 nike

我有这个字符串

[{\"label\":\"Issue Name\",\"value\":\"my dirst iOS \",\"_id\":\"issueName\"},{\"label\":\"Issue DueDate\",\"value\":\"15-12-2016\",\"_id\":\"dueDate\"}]

我想将它转换为 [NSDictionary] 类型;

[
{
"label": "Issue Name",
"value": "my dirst iOS ",
"_id": "issueName"
},
{
"label": "Issue DueDate",
"value": "15-12-2016",
"_id": "dueDate"
}
]

谁能告诉我该怎么做。我已经试过了How to convert a JSON string to a dictionary?

最佳答案

首先尝试删除斜杠

stringJson.stringByReplacingOccurrencesOfString("\\", withString: "")

然后用JsonConverter转换

func convertToDictionary(text: String) -> Any? {

if let data = text.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: data, options: []) as? Any
} catch {
print(error.localizedDescription)
}
}

return nil

}

然后

    if let list = self.convertToDictionary(text: stringJson) as? [AnyObject] {

print(list);
}

关于ios - 如何在 iOS Swift 3 中将 JSON 字符串转换为字典数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41163026/

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