I want to store JSON's response

Asked 1 years ago, Updated 1 years ago, 389 views

I would like to display JSON's response as String type in TableView using Codable as a structure.

var movies=MovieStruct() Missing argument for parameter 'from' in call (insert 'from'<#Decoder#>)

appears.Originally

 var movies= [MovieStruct]()

I put it in the array as above, but when I requested the API, it was not in the array (dictionary type?), and everything was returned with {}, so
It's not displayed well and I fixed the return of decodes as an array, but I'd like to store data from the structure in movies.
I don't know how to describe it.When I looked it up, I found out that all the API's were returned in an array, so I asked.

import UIKit

classViewController:UIViewController {
// https://api.themoviedb.org/3/movie/550?api_key =

    @IBOutlet weak var movieTableView: UITableView!
    
    var movies=MovieStruct()

    override func viewDidLoad(){
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        fechData()
    }
    
    func fechData(){

        let url=URL (string: "https://api.themoviedb.org/3/movie/550?api_key=")!
        URLSession.shared.dataTask(with:url){(data, response, error)in
            guard let data = data else {
                print(error?.localizedDescription???"Unknown error")
                return
            }
            
            let decoder = JSONDecoder()
            
            iflet movies=try?decoder.decode(MovieStruct.self, from:data){
                DispatchQueue.main.async {
//                    self.movies=movies
                    self.movies.append(movies)
                    self.movieTableView.reloadData()
                }
            } else {
                print("Unable parse JSON response")
            }
        }.resume()
    }
}

extensionViewController:UITableViewDelegate,UITableViewDataSource{
    
    functableView(_tableView:UITableView, numberOfRowsInSection section:Int) - > Int{
        return movies.count
    }
    
    functableView(_tableView:UITableView, cellForRowAtindexPath:IndexPath) - >UITableViewCell{
        letcell=tableView.dueReusableCell(withIdentifier: "Cell", for:indexPath)
        let movie=movies [indexPath.row]
        
        cell.textLabel?.text=movie.title
        cell.detailTextLabel?.text=movie.release_date
        
        return cell
    }
}

structureMovieStruct:Codable{
    variable —String
    var release_date —String
}

{
    adult = 0;
    "backdrop_path"="/8iVyhmjzUbvAGppkdCZPiyEHSoF.jpg";
    "belongs_to_collection"="<null>";
    budget=63000000;
    genres=(
                {
            id = 18;
            name = Drama;
        }
    );
    homepage="http://www.foxmovies.com/movies/fight-club";
    id = 550;
    "imdb_id" = tt0137523;
    "original_language" = en;
    "original_title" = "Fight Club";
    overview="Aticking-time-bomb insomniac and a slippery soap salesman primary male aggression into a shocking new form of therapy. Their concept matches on, with underground\"fight clubs\" forming in every town, until an event-outgoing sign of the tour
    popularity="39.218";
    "poster_path"="/wR5HZWdVpcXx9sevV1bQi7rP4op.jpg";
    "production_companies" =(
                {
            id = 508;
            "logo_path" = "/7PzJdsLGlR7oW4J0J5Xcd0pHGRg.png";
            name = "Regency Enterprises";
            "origin_country" = US;
        },
                {
            id = 711;
            "logo_path"="/tEiIH5QesdheJmDAqQwvtN60727.png";
            name = "Fox 2000 Pictures";
            "origin_country" = US;
        },
                {
            id = 20555;
            "logo_path" = "/hD8yEGUBLHOcfHYbujp71vD8gZp.png";
            name = "Taurus Film";
            "origin_country" = DE;
        },
                {
            id = 54051;
            "logo_path" = "<null>";
            name = "Atman Entertainment";
            "origin_country" = ";
        },
                {
            id = 54052;
            "logo_path" = "<null>";
            name = "Knickerbocker Films";
            "origin_country" = US;
        },
                {
            id = 25;
            "logo_path"="/qZCc1lty5FzX30aOCVRBLzaVmcp.png";
            name = "20th Century Fox";
            "origin_country" = US;
        },
                {
            id = 4700;
            "logo_path"="/A32wmjrs9Psf4zw0uaixF0GXfxq.png";
            name = "The Linson Company";
            "origin_country" = ";
        }
    );
    "production_countries" =(
                {
            "iso_3166_1" = DE;
            name = Germany;
        },
                {
            "iso_3166_1" = US;
            name = "United States of America";
        }
    );
    "release_date" = "1999-10-15";
    revise = 100853753;
    runtime = 139;
    "spoken_languages" =(
                {
            "iso_639_1" = en;
            name = English;
        }
    );
    status = Released;
    tagline="Mischief.Mayhem.Soap.";
    title="Fight Club";
    video=0;
    "vote_average" = "8.4";
    "vote_count" = 19688;
}

swift ios xcode

2022-09-30 21:50

1 Answers

First of all, it's not the actual response that you've been shown as a "response."It is a program that someone created, and after reading the response in NSDictionary, it appears that the NSDictionary was printed in the form of print.It's not completely useless, but it's often a hindrance to getting the correct answer because you can't restore the original JSON completely.

I'll show you what I actually got and shaped.

{
    "adult"—false,
    "backdrop_path": "/8iVyhmjzUbvAGppkdCZPiyEHSoF.jpg",
    "belongs_to_collection": null,
    "budget": 63000000,
    "genres": [
        {
            "id"—18,
            "name": "Drama"
        }
    ],
    "homepage": "http://www.foxmovies.com/movies/fight-club",
    "id" : 550,
    "imdb_id": "tt0137523",
    "original_language": "en",
    "original_title": "Fight Club",
    "overview": "Aticking-time-bomb insomniac and a slippery soap salesman primary male aggression into a shocking new form of therapy. Their concept matches on, with underground\"fight clubs\" forming in every town, until an event is out of sight.
    "popularity"—34.634,
    "poster_path": "/wR5HZWdVpcXx9sevV1bQi7rP4op.jpg",
    "production_companies":[
        {
            "id": 508,
            "logo_path": "/7PzJdsLGlR7oW4J0J5Xcd0pHGRg.png",
            "name": "Regency Enterprises",
            "origin_country": "US"
        },
        {
            "id" : 711,
            "logo_path": "/tEiIH5QesdheJmDAqQwvtN60727.png",
            "name": "Fox 2000 Pictures",
            "origin_country": "US"
        },
        {
            "id" : 20555,
            "logo_path": "/hD8yEGUBLHOcfHYbujp71vD8gZp.png",
            "name": "Taurus Film",
            "origin_country": "DE"
        },
        {
            "id" : 54051,
            "logo_path": null,
            "name": "Atman Entertainment",
            "origin_country": "
        },
        {
            "id" : 54052,
            "logo_path": null,
            "name": "Knickerbocker Films",
            "origin_country": "US"
        },
        {
            "id"—25,
            "logo_path": "/qZCc1lty5FzX30aOCVRBLzaVmcp.png",
            "name": "20th Century Fox",
            "origin_country": "US"
        },
        {
            "id" : 4700,
            "logo_path": "/A32wmjrs9Psf4zw0uaixF0GXfxq.png",
            "name": "The Linson Company",
            "origin_country": "
        }
    ],
    "production_countries":[
        {
            "iso_3166_1": "DE",
            "name": "Germany"
        },
        {
            "iso_3166_1": "US",
            "name": "United States of America"
        }
    ],
    "release_date": "1999-10-15",
    "revenue": 100853753,
    "runtime": 139,
    "spoken_languages": [
        {
            "iso_639_1": "en",
            "name": "English"
        }
    ],
    "status": "Released",
    "tagline": "Mischief.Mayhem.Soap.",
    title: "Fight Club",
    "video"—false,
    "vote_average"—8.4,
    "vote_count"—19690
}

Because this response is a JSON object, simply calling decode(_:from:) to this response will never result in an array.

In the first place, you can see this response (even in the original NSDictionary format) by carefully checking the contents, but you can only find information for one movie.Therefore, "I want to display ... in TableView" does not make sense.

What do you really want to display in this TableView?

I want each row of TableView to see a movie

複数Please check out the API that allows you to get multiple movie information and call it.

I want TableView to display multiple lines of information in each movie

一体Please find out what information you want to display.

I want to display information for one movie on one screen

TPlease design the screen without using TableView

You seem to be using the API that you are calling without understanding what information it will return.Is it an assignment for school or training for new recruits?

If you know a little more about what you really want to do, you may have more answers.


2022-09-30 21:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.