New to Swift. Need help mapping class files to JSON

Here is the top of the JSON and the first item of the array.

I want to use title, description, poster, and the link in videos.mp4.480

Answered by Claude31 in 726605022
struct Row: Codable {
  var title:String?
  var desciption:String?
  var Poster:String?
  struct videos: Codable {
    struct mp4: Codable {
      var _480: String?
    }
  }
}

This can compile but does not make sense. You declare struct (mp4) inside struct (videos) inside struct (row) but do not use them.

Maybe you want something like this (note that struct names should start with Uppercase.

struct MP4: Codable {
      var _480: String?
    }

  struct Videos: Codable {
    var mp4: MP4?
  }

struct row: Codable {
  var title:String?
  var desciption:String?
  var Poster:String?
  var videos: [Videos]
}

Why do you make everything optional ?

The JSON file, is weird. For instance "ttps://videos.kaiserclix.com/AVideo/videos/_YPTuniqid_5fbfd4096d6855.75762245_240.mp4" instead of https

You need to describe properly the JSON format in your struct, for instance, you would have an array of Row.

Learn how to do this. Here is a tutorial: https://www.raywenderlich.com/3418439-encoding-and-decoding-in-swift

Here is the two swift files I came up with.

//

//  videoFeed.swift

//  GetData

//

//  Created by Gregory Kaiser on 9/6/22.

//

import Foundation

struct videoFeed: Codable {

    var error: Bool

    var message: String?

    struct response: Codable{

        var totalRows: String?

        var rows: [row]

    }

Here is the second one

//

//  vids.swift

//  GetData

//

//  Created by Gregory Kaiser on 9/6/22.

//

import Foundation

struct row: Codable {

    var title:String?

    var desciption:String?

    var Poster:String?

    struct videos: Codable {

        struct mp4: Codable {

            var _480: String?

        }

    }

}

Can someone tell me if these are correct? And how do I access each item grabbing the link in videos.mp4.480?

Thank you!

}

Logon to YouTube. There are plenty of videos on the correct way to do JSON in Swift.

[https://www.youtube.com/results?search_query=json+and+swift)

Accepted Answer
struct Row: Codable {
  var title:String?
  var desciption:String?
  var Poster:String?
  struct videos: Codable {
    struct mp4: Codable {
      var _480: String?
    }
  }
}

This can compile but does not make sense. You declare struct (mp4) inside struct (videos) inside struct (row) but do not use them.

Maybe you want something like this (note that struct names should start with Uppercase.

struct MP4: Codable {
      var _480: String?
    }

  struct Videos: Codable {
    var mp4: MP4?
  }

struct row: Codable {
  var title:String?
  var desciption:String?
  var Poster:String?
  var videos: [Videos]
}

Why do you make everything optional ?

The JSON file, is weird. For instance "ttps://videos.kaiserclix.com/AVideo/videos/_YPTuniqid_5fbfd4096d6855.75762245_240.mp4" instead of https

You need to describe properly the JSON format in your struct, for instance, you would have an array of Row.

Learn how to do this. Here is a tutorial: https://www.raywenderlich.com/3418439-encoding-and-decoding-in-swift

Thank you so much for responding! So the missing 'h' was me removing it. I copied the JSON into Word with the hopes of covering up the private information and the urls and removed the 'h' to stop it appearing as link. When you a little bit of work that didn't need up working when attaching here. :-(

I have been watching/researching online to learn Swift and somebody suggesting making things optional if you are not sure if the key is always present.

I believe row is an array. It has 22 items.

Thank you for the tutorial! I will have a look.

So I changed the structure in attempt to follow the hierarchal tree because I keep getting nil when trying to print out what I thought would be the title. I have trying a lot of different lines of code just to print out one of the key value pairs. With no luck. Keep getting nil. I must be doing something wrong. Can some one help?

Here is the update swift file containing the structures.

//

//  claude31.swift

//  GetData

//

//  Created by Gregory Kaiser on 9/8/22.

//

import Foundation

struct MP4: Codable {

    var _480: String?

}

struct Videos: Codable {

    var mp4: MP4?

}

 

struct ROWS: Codable {

    var title:String?

    var description:String?

    var Poster:String?

}

struct RESP: Codable{

    var totalRows: Int

    var rows: [ROWS]

}

struct Kaiserclixresponse : Codable {

    var error: Bool

    var message: String?

    var resp: RESP

}

Here is the viewController code trying to access the JSON

//

//  ViewController.swift

//  GetData

//

//  Created by Gregory Kaiser on 9/6/22.

//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view.

        

        // Hit the API endpoint

        guard let url =  URL(string:"https://videos.kaiserclix.com/AVideo/plugin/API/get.json.php?APIName=video&user=gkaiser63@gmail.com&pass=abc123") else {

            fatalError("Invalid URL")

        }

                      

        URLSession.shared.dataTask(with: url) { (data, response, error) in

        

            // Check for errors

            guard let data = data, error == nil else {

                return

            }

            

            //Parse JSON

                        

            let result = try? JSONDecoder().decode(Kaiserclixresponse.self, from: data)

            if let result = result {

                print(result.resp.rows[0].Poster!)

            }

         }.resume()

     }

}

There is a first error:

  var resp: RESP

should be

  var response: RESP

In fact you should define coding keys

I read in JSON file

  response: {
       totalRows: 22
       rows: [ 22 items
0: {
  "id": 11,

is it really the content or have you added "22 items" and 0 ? Because that does not seem a valide JSON.

I may miss something, but the ROWS does not match at all the JSON.

Thank you! Greatly appreciated! Changing resp to response worked! I am pretty sure I read/heard the var names didn't have to match the JSON key names and I was seeing issues with a variable name response so I shortened it to resp.

Take care!

 I am pretty sure I read/heard the var names didn't have to match the JSON key names

That's correct, but only when you define coding keys, which is a good thing to do in many cases.

Good continuation and don't forget to close the thread by marking the correct answer.

Thanks again!

So I was successfully able to decode the JSON and I parse it but now I am having issues copying everything to array so I can assign them to UIImageView, UILabel and UIButton.

Can someone help again?

Here is the array definition

var videos: [String] = []

Here it the loop.

let result = try? JSONDecoder().decode(Kaiserclixresponse.self, from: data)

            if let result = result {

                result.response.rows.forEach {                                        let movie = $0                     videos.append(movie)

           {

but I keep getting

No exact matches in call to instance method 'append'

Open to suggestions on a better way of doing this.

New to Swift. Need help mapping class files to JSON
 
 
Q