To convert an array to JSON in Swift, you need to encode the array using JSONEncoder
. Here is an example:
main.swift264 chars10 lines
In the code above, we create an array of integers and encode it using JSONEncoder
. The encoded data is then converted to a string using String(data:encoding:)
method. Finally, the string is printed to the console.
To decode a JSON string back to an array, you can use JSONDecoder
. Here's an example:
main.swift295 chars10 lines
In the code above, we create a JSON string and convert it to Data
. We then decode the Data
using JSONDecoder
and print the resulting array to the console.
gistlibby LogSnag