Search code examples
javajsonswiftweb-serviceswebserver

Creating JSON data and parsing to swift


I have a problem with parsing my JSON data from my Webserver correctly. I tried to parse JSON data from files I found in the Internet and it worked fine but then I tried to create my one JSON data and tried to parse it in swift. The problem is that I can see the JSON data when I call the Adress in my Browser but when I try it in Swift it doesn't work. I also tried to debug to see what I get as responds and the Course Array is empty.

Here is my Java code:

@GET
@Path("/course")
@Produces(MediaType.APPLICATION_JSON)
public List getCourse(){
    List courseList = new ArrayList();
    Course pCourse = new Course(0, "name", "ll", null);
    courseList.add(pCourse);

    return courseList;
}

"Course" data from Java:

public int id;
public String name;
public String link;
public String imageUrl;

public Course() {
}

public Course(int id, String name, String link, String imageUrl) {
    this.id = id;
    this.name = name;
    this.link = link;
    this.imageUrl = imageUrl;
}

Here is my Swift code:

URLSession.shared.dataTask(with: costumeUrl) { (data, response, err) in
    guard let data = data else{ return}
//            let dataString = String(data: data, encoding: .utf8)
//            print(dataString)
    do{
        let course = try JSONDecoder().decode([Course].self, from: data)
        print(course)
    }catch let jsonError{
        print(jsonError)
    }
    }.resume()

"Course" data from Swift:

struct Course: Decodable {
    let id: Int
    let name: String
    let link: String
    let imageUrl: String

    init(json: [String: Any]){
        id = json["id"] as? Int ?? -1
        name = json["name"] as? String ?? ""
        link = json["link"] as? String ?? ""
        imageUrl = json["imageUrl"] as? String ?? ""

    }
}

And here is the Responds in my browser:

[{"id":0,"imageUrl":null,"link":"ll","name":"name"}]

If you have any questions or need any additional Information please ask. Thank you.


Solution

  • Have a try with this "Course"-model:

    Care: Use decodeIfPresent if the value from your JSON-Response can be null.

    class Course: Decodable {
        let id: Int
        let name: String
        let link: String
        let imageUrl: String?
    
        private enum CourseCodingKeys: String, CodingKey {
            case id = "id"
            case name = "name"
            case link = "link"
            case imageUrl = "imageUrl"
        }
    
        required init(from decoder: Decoder) throws {
            let courseContainer = try decoder.container(keyedBy: CourseCodingKeys.self)
            self.id = try courseContainer.decode(Int.self, forKey: .id)
            self.name = try courseContainer.decode(String.self, forKey: .name)
            self.link = try courseContainer.decode(String.self, forKey: .link)
            self.imageUrl = try courseContainer.decodeIfPresent(String.self, forKey: .imageUrl)
        }
    }