Last active
October 21, 2023 13:13
-
-
Save StewartLynch/0f9edf5bf5b0a149b2059494401fb36d to your computer and use it in GitHub Desktop.
A JSON Parsing example for the dictionary api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
struct Post: Decodable { | |
let word: String | |
let meanings:[Meanings] | |
struct Meanings: Decodable { | |
let partOfSpeech: String? | |
let definitions: [Definitions] | |
} | |
struct Definitions: Decodable { | |
let definition: String | |
let example: String? | |
} | |
} | |
func getWord(_ wordString: String) { | |
var urlString = "https://api.dictionaryapi.dev/api/v2/entries/en/\(wordString)" | |
urlString = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)! | |
print(urlString) | |
guard let url = URL(string:urlString) else { | |
return | |
} | |
let request = URLRequest(url: url) | |
URLSession.shared.dataTask(with: request) { data, response, error in | |
if let error = error { | |
print(error.localizedDescription) | |
return | |
} | |
guard let data = data else { | |
return | |
} | |
let decoder = JSONDecoder() | |
guard let posts = try? decoder.decode([Post].self, from: data) else { | |
print("Could not decode") | |
return } | |
for post in posts { | |
print("Word: \(post.word)") | |
print("Definitions:") | |
for meaning in post.meanings { | |
for definition in meaning.definitions { | |
print("• \(definition.definition)") | |
if let example = definition.example { | |
print("Example: \(example)") | |
} | |
} | |
} | |
} | |
}.resume() | |
} | |
getWord("search") |
You are welcome. I don’t even remember doing this. It must have been some time ago. I am curious. How did you find it?
Stewart Lynch
Web: https://www.createchsol.com
YouTube: ***@***.***
X: https:/x.com/StewartLynch
Mastodon: ***@***.***
… On Oct 20, 2023, at 11:00 AM, Renat3000 ***@***.***> wrote:
@Renat3000 commented on this gist.
hey! I just want to thank you for this bit!
I was facing one problem, was trying to decode json from the same api, but had an error:
typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Dictionary<String, Any> but found an array instead.", underlyingError: nil))
I changed Post.self to [Post].self, cause we need to pass an array type to JSONDecoder.decode and it works now! Thanks you very much 🙌🏻🙏🏻
—
Reply to this email directly, view it on GitHub <https://gist.github.com/StewartLynch/0f9edf5bf5b0a149b2059494401fb36d#gistcomment-4733303> or unsubscribe <https://github.com/notifications/unsubscribe-auth/AAWN77BADRFAFEZAFWARD6LYAK35FBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFQKSXMYLMOVS2I5DSOVS2I3TBNVS3W5DIOJSWCZC7OBQXE5DJMNUXAYLOORPWCY3UNF3GS5DZVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTANBYGI3TOMJQU52HE2LHM5SXFJTDOJSWC5DF>.
You are receiving this email because you authored the thread.
Triage notifications on the go with GitHub Mobile for iOS <https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675> or Android <https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
I am curious. How did you find it?
well, given my little experience with APIs I haven't seen the APIs which have an array at the very first "level" and I thought "that's strange, maybe if I google how to decode this particular api.dictionaryapi.dev/api/v2 I will find some solution" and here I am!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hey! I just want to thank you for this bit!
I was facing one problem, was trying to decode json from the same api, but had an error:
I changed Post.self to [Post].self, cause we need to pass an array type to JSONDecoder.decode and it works now! Thank you very much 🙌🏻🙏🏻