Skip to content

Instantly share code, notes, and snippets.

@4np
Created March 10, 2017 13:35
Show Gist options
  • Save 4np/1d96c5d06be69cfc59749f753fb46458 to your computer and use it in GitHub Desktop.
Save 4np/1d96c5d06be69cfc59749f753fb46458 to your computer and use it in GitHub Desktop.
Creating JSON in Swift (Playground)
//: Playground - noun: a place where people can play
import UIKit
//[{
// "508d49e5d7200200b96d45a3ce6103c4":[{ // filter widget id
// "table":"incident",
// "filter":"categoryINsoftware"
// }],
// "id":”508d49e5d7200200b96d45a3ce6103c4” // filter widget id
//}]
let filterParameters: [String: String] = [
"table": "incident",
"filter": "categoryINsoftware"
]
let filter: [String: Any] = [
"508d49e5d7200200b96d45a3ce6103c4": [filterParameters],
"id": "508d49e5d7200200b96d45a3ce6103c4"
]
let json: [Any] = [filter]
let valid = JSONSerialization.isValidJSONObject(json) // true
do {
let jsonData = try JSONSerialization.data(withJSONObject: json, options: [])
let jsonString = String(data: jsonData, encoding: .utf8)!
print(jsonString)
} catch let error {
print("error")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment