Post

Replies

Boosts

Views

Activity

How do I declare gif and media objects?
I'm setting up an API call to Tenor.com, docs here https://tenor.com/gifapi/documentation#responseobjects-gif . I'm setting up a struct for my returning JSON, and I'm stuck on the media part. For "media" it says to use "[ { GIF_FORMAT : MEDIA_OBJECT } ]". How do I declare gif format and media objects? Or is there another way to set this up? Here's what I've got do far. struct structForAllApiResults: Codable { // MARK: - Gif Object let created: Float // a unix timestamp representing when this post was created. let hasaudio: Bool // true if this post contains audio (only video formats support audio, the gif image file format can not contain audio information). let id: String //Tenor result identifier let media: [ Dictionary<GIF,>] // An array of dictionaries with GIF_FORMAT as the key and MEDIA_OBJECT as the value let tags: [String] // an array of tags for the post let title: String // the title of the post. let itemurl: String // the full URL to view the post on tenor.com. let hascaption: Bool // true if this post contains captions let url: String // a short URL to view the post on tenor.com. // MARK: - Category Object let searchterm: String let path: String let image: String let name: String // MARK: - Media Object let preview: String let url: String let dims: [Int] // dimensions // MARK: - Format Types let gif: }
0
0
551
Jul ’21
How can I identify and group data in a JSON file?
I have a large JSON file with a top level array: [ { "tag" : "AF", "geopoliticalarea" : "Afghanistan", "travel_transportation" : etc... } ] The JSON file is a list of every country along with info for each country. You can see it here if you'd like: https://cadatacatalog.state.gov/dataset/4a387c35-29cb-4902-b91d-3da0dc02e4b2/resource/299b3b67-3c09-46a3-9eb7-9d0086581bcb/download/countrytravelinfo.json I've set up a struct for the JSON info: struct ReceivedAPIData: Codable { let tag: String let geopoliticalarea: String let travel_transportation: String // with html let health: String // with html let destination_description: String // with html let iso_code: String let travel_embassyAndConsulate: String // with html let last_update_date: String } (Initially I wanted to use an API call but if it's easier to save all of the data locally I'm not opposed to that). What I'm having trouble with is identifying/grouping/and pulling out all the the above info for one country at a time. The entire file is in an array but I don't know how to access each country. If I look at the link with Firefox I can see that each country is assigned a number, 0 - 210, but maybe that's just Firefox helping out? I don't see any numbers like that in the raw JSON. Here's my API call func: guard let apiUrl = URL(string: "https://cadatacatalog.state.gov/dataset/4a387c35-29cb-4902-b91d-3da0dc02e4b2/resource/299b3b67-3c09-46a3-9eb7-9d0086581bcb/download/countrytravelinfo.json") else { print("Error: cannot create URL.") return } let session = URLSession(configuration: .default) let task = session.dataTask(with: apiUrl) { [self] data, response, error in let decoder = JSONDecoder() guard let safeData = data else { print("Couldn't get data back from url."); return } guard let safeDecodeData = try? decoder.decode([ReceivedAPIData].self, from: safeData) else { fatalError("Couldn't decode data.")} self.delegate?.didUpdateAPIResults(returnedResult: safeDecodeData) let jsonStringData = data?.prettyPrintedJSONString print("Pretty JSON from server ----- \(String(describing: jsonStringData))") // Use this to remove the HTML from the string data later safeDecodeData.map -> { ReceivedAPIData in // // } if let e = error { print(e.localizedDescription) } callCompletionHandler(safeDecodeData) } task.resume() } } My goal for this app is to search by country name in a text field and have only the info for that county displayed. I've googled around about top level arrays and nested JSON info but can't figure out what my next step is. Thanks.
2
0
1.1k
Jun ’21
How do I declare gif and media objects?
I'm setting up an API call to Tenor.com, docs here https://tenor.com/gifapi/documentation#responseobjects-gif . I'm setting up a struct for my returning JSON, and I'm stuck on the media part. For "media" it says to use "[ { GIF_FORMAT : MEDIA_OBJECT } ]". How do I declare gif format and media objects? Or is there another way to set this up? Here's what I've got do far. struct structForAllApiResults: Codable { // MARK: - Gif Object let created: Float // a unix timestamp representing when this post was created. let hasaudio: Bool // true if this post contains audio (only video formats support audio, the gif image file format can not contain audio information). let id: String //Tenor result identifier let media: [ Dictionary<GIF,>] // An array of dictionaries with GIF_FORMAT as the key and MEDIA_OBJECT as the value let tags: [String] // an array of tags for the post let title: String // the title of the post. let itemurl: String // the full URL to view the post on tenor.com. let hascaption: Bool // true if this post contains captions let url: String // a short URL to view the post on tenor.com. // MARK: - Category Object let searchterm: String let path: String let image: String let name: String // MARK: - Media Object let preview: String let url: String let dims: [Int] // dimensions // MARK: - Format Types let gif: }
Replies
0
Boosts
0
Views
551
Activity
Jul ’21
How can I identify and group data in a JSON file?
I have a large JSON file with a top level array: [ { "tag" : "AF", "geopoliticalarea" : "Afghanistan", "travel_transportation" : etc... } ] The JSON file is a list of every country along with info for each country. You can see it here if you'd like: https://cadatacatalog.state.gov/dataset/4a387c35-29cb-4902-b91d-3da0dc02e4b2/resource/299b3b67-3c09-46a3-9eb7-9d0086581bcb/download/countrytravelinfo.json I've set up a struct for the JSON info: struct ReceivedAPIData: Codable { let tag: String let geopoliticalarea: String let travel_transportation: String // with html let health: String // with html let destination_description: String // with html let iso_code: String let travel_embassyAndConsulate: String // with html let last_update_date: String } (Initially I wanted to use an API call but if it's easier to save all of the data locally I'm not opposed to that). What I'm having trouble with is identifying/grouping/and pulling out all the the above info for one country at a time. The entire file is in an array but I don't know how to access each country. If I look at the link with Firefox I can see that each country is assigned a number, 0 - 210, but maybe that's just Firefox helping out? I don't see any numbers like that in the raw JSON. Here's my API call func: guard let apiUrl = URL(string: "https://cadatacatalog.state.gov/dataset/4a387c35-29cb-4902-b91d-3da0dc02e4b2/resource/299b3b67-3c09-46a3-9eb7-9d0086581bcb/download/countrytravelinfo.json") else { print("Error: cannot create URL.") return } let session = URLSession(configuration: .default) let task = session.dataTask(with: apiUrl) { [self] data, response, error in let decoder = JSONDecoder() guard let safeData = data else { print("Couldn't get data back from url."); return } guard let safeDecodeData = try? decoder.decode([ReceivedAPIData].self, from: safeData) else { fatalError("Couldn't decode data.")} self.delegate?.didUpdateAPIResults(returnedResult: safeDecodeData) let jsonStringData = data?.prettyPrintedJSONString print("Pretty JSON from server ----- \(String(describing: jsonStringData))") // Use this to remove the HTML from the string data later safeDecodeData.map -> { ReceivedAPIData in // // } if let e = error { print(e.localizedDescription) } callCompletionHandler(safeDecodeData) } task.resume() } } My goal for this app is to search by country name in a text field and have only the info for that county displayed. I've googled around about top level arrays and nested JSON info but can't figure out what my next step is. Thanks.
Replies
2
Boosts
0
Views
1.1k
Activity
Jun ’21