[英]how to convert JSON-String into Array of Dictionaries in iOS Swift 3
I have this String我有这个字符串
[{\"label\":\"Issue Name\",\"value\":\"my dirst iOS \",\"_id\":\"issueName\"},{\"label\":\"Issue DueDate\",\"value\":\"15-12-2016\",\"_id\":\"dueDate\"}]
I want to convert it to type [NSDictionary]
for example;例如,我想将其转换为
[NSDictionary]
类型;
[
{
"label": "Issue Name",
"value": "my dirst iOS ",
"_id": "issueName"
},
{
"label": "Issue DueDate",
"value": "15-12-2016",
"_id": "dueDate"
}
]
Can someone tell me how to do that.谁能告诉我怎么做。 I Have Already Tried How to convert a JSON string to a dictionary?
我已经尝试过如何将 JSON 字符串转换为字典?
first try to remove the slashes首先尝试删除斜线
stringJson.stringByReplacingOccurrencesOfString("\\", withString: "")
then convert it with JsonConverter然后用 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
}
then然后
if let list = self.convertToDictionary(text: stringJson) as? [AnyObject] {
print(list);
}
Swift 5 Easy way Swift 5 简单方法
//MARK:- Calling
if let list = self.convertToDictionary(text: stringJson) as? [AnyObject] {
print(list);
}
//MARK:- Remove the Slashes
let text = stringJson.replacingOccurrences(of: "\\", with: "")
//MARK:- Convert it with 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
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.