I am trying to parse a JSON string which is a collection of multiple 'Countries'. I have created a structure representing the fields in each JSON object and then one for the array. However I keep getting the following error
Error: cannot convert value of type 'CountryList' to specified type 'Country'
Here is my Code (written in Swift Playgrounds)
import Cocoa
let body = """
{"Countries":
[
{
"id": 1,
"name": "USA",
"capitalCity": 5,
},
{
"id": 2,
"name": "France",
"capitalCity": 5,
}]}
""".data(using: .utf8)
struct Country: Codable {
let id: Int
let name: String
let capitalCity: Int
}
struct CountryList: Codable {
var Countries: [Country]
}
let countryJson: Country = try! JSONDecoder().decode(CountryList.self, from: body!)
print(countryJson)
Thank you for your help.
Replace this line
let countryJson: Country = try! JSONDecoder().decode(CountryList.self, from: body!)
with this:
let countryJson: CountryList = try! JSONDecoder().decode(CountryList.self, from: body!)
Related
I am pretty new to Swift and as an exercise I try to create an App with Swift 5, which should show you the Weather at a Location you searched for.
Right now I am trying to implement a function that can turn a local JSON file in a struct I am using.
The JSON file is located at the same directory as all the other files.
The JSON for testing purposes looks like this:
[
{
"id": 833,
"name": "Ḩeşār-e Sefīd",
"state": "",
"country": "IR",
"coord": {
"lon": 47.159401,
"lat": 34.330502
}
},
{
"id": 2960,
"name": "‘Ayn Ḩalāqīm",
"state": "",
"country": "SY",
"coord": {
"lon": 36.321911,
"lat": 34.940079
}
}
]
The struct:
struct Root: Codable {
let jsonWeather: [JsonWeather]?
}
struct JsonWeather: Codable {
let id: Int
let name: String
let state: String
let country: String
let coord: Coord
enum CodingKeys: String, CodingKey {
case id = "id"
case name = "name"
case state = "state"
case country = "country"
case coord = "coord"
}
}
The function that i am working on:
func loadJson(fileName: String) -> Void{
if let url = Bundle.main.url(forResource: fileName, withExtension: "json") {
do {
let data = try Data(contentsOf: url)
let jsonData = try JSONDecoder().decode(Array<Root>.self, from: data)
print(type(of: jsonData)) // Array<Root>
print(jsonData) // [WeatherApp.Root(jsonWeather: nil), WeatherApp.Root(jsonWeather: nil)]
} catch {
print("error:\(error)")
}
}
}
After all I wanted to see how the result looks like and I noticed that each jsonWeather in Root is nil
So in the end I understand that the values are nil because I allow them to be nil by setting an optional in the Root struct, but I dont quite understand why they are turning nil because there is data given. Also I don't know how I would implement it without being an optional.
This is my first time using JSONDecoder and Optionals in Swift.
Could anyone point out what I did wrong (or understood wrong) and how to fix this issue
Since your JSON is just of array of objects, you don't need a top-level struct like Root.
Get rid of your Root struct, and just decode an array of JsonWeathers:
let jsonData = try JSONDecoder().decode([JsonWeather].self, from: data)
(Note that there is no difference between [JsonWeather].self and Array<JsonWeather>.self - I just chose to do it this way since it's shorter.)
I am trying to write a Swift Codable model for the below JSON.
{
"batchcomplete": "",
"query": {
"pages": {
"26667" (The problem is here): {
"pageid": 26667,
"ns": 0,
"title": "Spain",
"contentmodel": "wikitext",
"pagelanguage": "en",
"pagelanguagehtmlcode": "en",
"pagelanguagedir": "ltr",
"touched": "2020-03-14T18:03:48Z",
"lastrevid": 945549863,
"length": 254911,
"fullurl": "https://en.wikipedia.org/wiki/Spain",
"editurl": "https://en.wikipedia.org/w/index.php?title=Spain&action=edit",
"canonicalurl": "https://en.wikipedia.org/wiki/Spain"
}
}
}
}
The problem is that one of the key changes every time I query.
Marked the in the above JSON as (The problem is here)
How to parse the above JSON file with JSONDecoder?
This can be easily parsed with libraries like SwiftyJSON.
The point is in making let pages: [String:Item] Use
// MARK: - Root
struct Root: Codable {
let batchcomplete: String
let query: Query
}
// MARK: - Query
struct Query: Codable {
let pages: [String:Item]
}
// MARK: - Item
struct Item: Codable {
let pageid, ns: Int
let title, contentmodel, pagelanguage, pagelanguagehtmlcode: String
let pagelanguagedir: String
let touched: Date
let lastrevid, length: Int
let fullurl: String
let editurl: String
let canonicalurl: String
}
let res = try JSONDecoder().decode(Root.self, from: data)
I have a very long Json array that is full of items that look like this:
[
{
"id": "sm10-1",
"name": "Pheromosa & Buzzwole-GX",
"imageUrl": "https://images.pokemontcg.io/sm10/1.png",
"subtype": "TAG TEAM",
"supertype": "Pokémon",
"hp": "260",
"retreatCost": [
"Colorless",
"Colorless"
],
"convertedRetreatCost": 2,
"number": "1",
"artist": "Mitsuhiro Arita",
"rarity": "Rare Holo GX",
"series": "Sun & Moon",
"set": "Unbroken Bonds",
"setCode": "sm10",
"text": [
"When your TAG TEAM is knocked out, your opponent takes 3 Prize Cards."
],
"types": [
"Grass"
],
"attacks": [
{
"name": "Jet Punch",
"cost": [
"Grass"
],
"convertedEnergyCost": 1,
"damage": "30",
"text": "This attack does 30 damage to 1 of your opponent's Benched Pokémon. (Don't apply Weakness and Resistance for Benched Pokémon.)"
},
{
"name": "Elegant Sole",
"cost": [
"Grass",
"Grass",
"Colorless"
],
"convertedEnergyCost": 3,
"damage": "190",
"text": "During your next turn, this Pokémon's Elegant Sole attack's base damage is 60."
},
{
"name": "Beast Game-GX",
"cost": [
"Grass"
],
"convertedEnergyCost": 1,
"damage": "50",
"text": "If your opponent's Pokémon is Knocked Out by damage from this attack, take 1 more Prize card. If this Pokémon has at least 7 extra Energy attached to it (in addition to this attack's cost), take 3 more Prize cards instead. (You can't use more than 1 GX attack in a game.)"
}
],
"weaknesses": [
{
"type": "Fire",
"value": "×2"
}
],
"imageUrlHiRes": "https://images.pokemontcg.io/sm10/1_hires.png",
"nationalPokedexNumber": 794
}
]
That is just one item of hundreds in the array. What I want to do is grab specific values from each item (i.e. name, imageUrl, supertype, hp, rarity, set) and send them to a struct which will then be added to an array of such structs.
What I currently have prints just prints out all of the json data and I can not figure out how to get individual data and create an array of structs for each individual card.
Here is the code I have currently:
//[TEST] READING JSON FILE LOCALLY
struct card: Decodable {
let name: String
let imageUrl: String
let supertype: String
let artist: String
let rarity: String
let set: String
let types: Array<String>
}
func loadJsonInfo() {
do{
let data = try Data.init(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: "Unbroken Bonds", ofType: "json")!))
let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
print(json)
} catch {
print(error)
}
}
Also, the json file is locally stored in my appData. Thanks in advance for your help!
Give a try to https://quicktype.io/
You put json there. And get all necessary data structures to decode json
To decode a JSON with Decodable type, you need to use JSONDecoder's decode(_:from:) method.
Update your loadJsonInfo() method to,
func loadJsonInfo() {
if let file = Bundle.main.url(forResource: "Unbroken Bonds", withExtension: "json") {
do {
let data = try Data(contentsOf: file)
let arr = try JSONDecoder().decode([Card].self, from: data)
print(arr)
} catch {
print(error)
}
}
}
Note: Use first letter capital while creating a type, i.e. use Card instead of card.
Parsing Code when you have DATA from server. I also removed force unwrapped ! so it won't crash in absence of file
func loadJsonInfo() {
if let path = Bundle.main.path(forResource: "Unbroken Bonds", ofType: "json") {
do {
let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .alwaysMapped)
let result = try JSONDecoder().decode(ResultElement.self, from: data)
} catch let error {
print(error)
}
}
}
Your Model
import Foundation
// MARK: - ResultElement
struct ResultElement: Codable {
let id, name: String?
let imageURL: String?
let subtype, supertype, hp: String?
let retreatCost: [String]?
let convertedRetreatCost: Int?
let number, artist, rarity, series: String?
let resultSet, setCode: String?
let text, types: [String]?
let attacks: [Attack]?
let weaknesses: [Weakness]?
let imageURLHiRes: String?
let nationalPokedexNumber: Int?
enum CodingKeys: String, CodingKey {
case id, name
case imageURL = "imageUrl"
case subtype, supertype, hp, retreatCost, convertedRetreatCost, number, artist, rarity, series
case resultSet = "set"
case setCode, text, types, attacks, weaknesses
case imageURLHiRes = "imageUrlHiRes"
case nationalPokedexNumber
}
}
// MARK: - Attack
struct Attack: Codable {
let name: String?
let cost: [String]?
let convertedEnergyCost: Int?
let damage, text: String?
}
// MARK: - Weakness
struct Weakness: Codable {
let type, value: String?
}
typealias Result = [ResultElement]
I'm calling the API endpoint and the JSON response looks like this:
{
"id": 299536,
"cast": [
{
"cast_id": 1,
"character": "Tony Stark / Iron Man",
"credit_id": "54a9cfa29251414d5b00553d",
"gender": 2,
"id": 3223,
"name": "Robert Downey Jr.",
"order": 0,
"profile_path": "/1YjdSym1jTG7xjHSI0yGGWEsw5i.jpg"
},
{
"cast_id": 6,
"character": "Thor Odinson",
"credit_id": "54a9d012c3a3680c29005762",
"gender": 2,
"id": 74568,
"name": "Chris Hemsworth",
"order": 1,
"profile_path": "/lrhth7yK9p3vy6p7AabDUM1THKl.jpg"
},
{
"cast_id": 13,
"character": "Bruce Banner / Hulk",
"credit_id": "573fc00592514177ec00010a",
"gender": 2,
"id": 103,
"name": "Mark Ruffalo",
"order": 2,
"profile_path": "/isQ747u0MU8U9gdsNlPngjABclH.jpg"
}
]
This is the struct that I've made for the codable protocol.
struct MovieCast: Codable {
let id: Int
let cast: [Cast]
let crew: [Crew]
}
struct Cast: Codable {
let castID: Int
let character, creditID: String
let gender, id: Int
let name: String
let order: Int
let profilePath: String?
enum CodingKeys: String, CodingKey {
case castID = "cast_id"
case character
case creditID = "credit_id"
case gender, id, name, order
case profilePath = "profile_path"
}
}
struct Crew: Codable {
let creditID: String
let department: Department
let gender, id: Int
let job, name: String
let profilePath: String?
enum CodingKeys: String, CodingKey {
case creditID = "credit_id"
case department, gender, id, job, name
case profilePath = "profile_path"
}
}
enum Department: String, Codable {
case art = "Art"
case camera = "Camera"
case costumeMakeUp = "Costume & Make-Up"
case crew = "Crew"
case directing = "Directing"
case editing = "Editing"
case lighting = "Lighting"
case production = "Production"
case sound = "Sound"
case visualEffects = "Visual Effects"
case writing = "Writing"
}
My question is, how can I loop through the objects in a single array using Swift 4? I am using Decodable protocol and have struct that looks like this: (note that I let out some of the json response properties above so the snippet wouldn't be too long)
My goal is to get the
profile_path
out of every object and append it to the array of Strings.
After you get the response try this code in viewDidLoad()
if let dict = response.result.value as? [string:Anyobject]() //fetching the response
if let innerdict = dict["cast"] as? [[string:Anyobject]]()
for cast in innerdict {
if let profile_path = cast[ "profile_path"] as? String
print("profile_path") // to check the profilepath
}
// this is it to get the profile path then append it in the array of type string
Your MovieCast struct contains 2 arrays, one of Cast objects and one of Crew objects.
You could create a protocol Staff that has the shared properties from both (gender, name, profilePath), declare that both types conform to that protocol, and then combine your arrays of Cast and Crew objects into an array of Staff.
Then you could map the resulting array to an array of strings from the profilePath values of each entry in the array:
protocol Staff {
var gender: Int { get }
var name: String { get }
var profilePath: String? { get }
}
Change Cast and Crew like this:
struct Cast: Codable & Staff
struct Crew: Codable & Staff
if you have a crew:
var movieCrew: MovieCast
//Code to load a moview crew
Then
let castAndCrew = (movieCrew.cast as [Staff]) + ((movieCrew.crew as [Staff]?) ?? [])
let allProfilePaths = castAndCrew.compactMap { $0.profilePath }
(Note that I typed this into the SO editor, and my syntax is rarely perfect "out of the gate." It will likely need some minor cleanup.)
I am trying to update myself and use modern and recent Swift 4 features.
That is why I am training with the Codable protocol in order to parse JSON and directly map my model object.
First of all, I did some research and self learning.
This article helped me a lot : Ultimate guide
I just need to focus on the "Com" array.
As you can notice, it contains some nested object. I named them Flash Info.
It is defined by :
endDate
text
image[]
title
productionDate
id
So here is my Codable Struct :
struct FlashInfo : Codable {
let productionDate: String
let endDate: String
let text: String
let title: String
let id: String
}
First of all, I was trying to parse it without the array of Images, I will handle it later.
So here is my method :
func getFlashInfo(success: #escaping (Array<FlashInfo>) -> Void) {
var arrayFlash = [FlashInfo]()
Alamofire.request(URL_TEST, method: .get).responseJSON { response in
if response.value != nil {
if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
print("Data: \(utf8Text)")
}
//let decoder = JSONDecoder()
// let flash = try! decoder.decode(FlashInfo.self, from: response.data!)
// arrayFlash.append(flash)
success(arrayFlash)
} else {
print("error getFlashInfo")
}
}
}
I don't know how to handle the fact that I only need the "Com" array and how to iterate through all nested objects in order to fill my array in the callback.
I mean, will the decode protocol iterate through each objects ?
Am I clear?
EDIT: JSON as text
{"Test": [], "Toto": [], "Com": [{"endDate": "2017-06-27T08:00:00Z", "text": "John Snow is getting married", "image": ["895745-test.png", "632568-test.png"], "titre": "We need you!", "productionDate": "2017-07-02T16:16:23Z", "id": "9686"}, {"endDate": "2017-07-27T08:00:00Z", "text": "LOL TEST", "image": ["895545-test.png", "632568-test.png"], "titre": "She needs you!", "productionDate": "2017-08-02T16:16:23Z", "id": "9687"},{"endDate": "2017-06-27T08:00:00Z", "text": "iOS swift", "image": ["895775-test.png", "638568-test.png"], "titre": "They need you!", "productionDate": "2017-07-02T16:16:23Z", "id": "9688"}], "Yt": []}
I believe the quickest way is just to define an incomplete Response type as well. For instance:
struct Response: Codable {
let Com: [FlashInfo]
}
struct FlashInfo: Codable {
let productionDate: String
let endDate: String
let text: String
let title: String
let id: String
let image: [String] = [] // Ignored for now.
enum CodingKeys: String, CodingKey {
case productionDate, endDate, text, id
case title = "titre" // Fix for JSON typo ;)
}
}
and decode it like this:
let decoder = JSONDecoder()
let response = try! decoder.decode(Response.self, from: data)
print(response.Com)
This worked great with the test data you provided (just watch out for the typo in the title field):
let json = """
{"Test": [], "Toto": [], "Com": [{"endDate": "2017-06-27T08:00:00Z", "text": "John Snow is getting married", "image": ["895745-test.png", "632568-test.png"], "titre": "We need you!", "productionDate": "2017-07-02T16:16:23Z", "id": "9686"}, {"endDate": "2017-07-27T08:00:00Z", "text": "LOL TEST", "image": ["895545-test.png", "632568-test.png"], "titre": "She needs you!", "productionDate": "2017-08-02T16:16:23Z", "id": "9687"},{"endDate": "2017-06-27T08:00:00Z", "text": "iOS swift", "image": ["895775-test.png", "638568-test.png"], "titre": "They need you!", "productionDate": "2017-07-02T16:16:23Z", "id": "9688"}], "Yt": []}
"""
let data = json.data(using: .utf8)!