Reputation: 59
Im trying to covert this string into a dictionary
{
"sender_id": 7,
"Sender_name": Testchumthree Tester,
"message": 42,
"Sender_image": https://graph.facebook.com/v2.10/281359099024687/picture?type=normal,
"timestamp": "0",
"group_id": 50
}
Below is what i found so far.
func convertToDictionary(text: String) -> [String: Any]? {
if let data = text.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
} catch {
print(error.localizedDescription)
}
}
return nil
}
But this i get an error saying the data is not in the correct format. Any help would be much appreciated.
Upvotes: 0
Views: 1272
Reputation: 9140
The problem with your JSON is that strings are not properly formatted. There must be quotes around them and depending on the library "/" must also be escaped.
Try using this:
{
"sender_id": 7,
"Sender_name": "Testchumthree Tester",
"message": 42,
"Sender_image": "https:\/\/graph.facebook.com\/v2.10\/281359099024687\/picture?type=normal",
"timestamp": "0",
"group_id": 50
}
Upvotes: 1