JSONEncoder code example
Example 1: python json dump
import json
with open("data_file.json", "w") as write_file:
json.dump(data, write_file)
Example 2: JSONDecoder
struct GroceryProduct: Codable {
var name: String
var points: Int
var description: String?
}
let json = """
{
"name": "Durian",
"points": 600,
"description": "A fruit with a distinctive scent."
}
""".data(using: .utf8)!
let decoder = JSONDecoder()
let product = try decoder.decode(GroceryProduct.self, from: json)
print(product.name) // Prints "Durian"