Python: Json Data into Class Instance varaibles
<p>I am trying to create a class called Movie. I have declared the instance variables. I am calling the OMDB API and I would like to store assign the variables to that. But that doesn't seem to be working. Even when I print json_Data, it doesn't print anything. Can anyone point me in the right direction. I know I can store the data into a dictionary. But how would it be stored in a class. I am a newbiew that is still learning python. </p>

<pre><code>class Movie(object):
    """ Class provides a structure to store Movie information """



    def __init__(self, imdb_id, title = None, release_year = None, rating = None, run_time = None, genre = None, director = None, actors = None, plot = None, awards = None, poster_image = None, imdb_votes = None, youtube_trailer = None):
        self.imdb_id = imdb_id
        self.title = title
        self.release_year = release_year
        self.rating = rating
        self.run_time = run_time
        self.genre = genre
        self.director = director
        self.actors = actors
        self.plot = plot
        self.awards = awards
        self.poster_image = poster_image
        self.imdb_votes = imdb_votes
        self.youtube_trailer = youtube_trailer


    def get_api_data(self):
        """
            Method retreves and parses information for each movie based on imdb_id
        """
            #URL for OMDBI
        url = "http://www.omdbapi.com/?i="+self.imdb_id+"&amp;plot=full&amp;r=json&amp;v=1"
        try:
            response = urllib.urlopen(url)

        except URLERROR as e:
            if hasattr(e, 'reason'):
                print ("Unable to reach a server.")
                print 'Reason: ', e.reason
            elif hasattr(e, 'code'):
                print "Server is unable to fulfill the request."
                print 'Error Code: ', e.code
            else:
                json_data = json.loads(response.read())
                self.imdb_id = json_data["imdbID"].encode("utf8","ignore")
                self.title = json_data["Title"].encode("utf8","ignore")
                self.release_year = int(json_data["Released"].split("")[-1])
                self.rating = json_data["imdbRating"].encode("utf8", "ignore")
                self.run_time = json_data["Runtime"].encode("utf8", "ignore")
                self.genre = json_data["Rated"].encode("utf8", "ignore")
                self.director = json_data["Director"].encode("utf8", "ignore")
                self.actors = json_data["Actors"].encode("utf8", "ignore")
                self.plot = json_data["Plot"].encode("utf8", "ignore")
                self.awards = json_data["Awards"].encode("utf8", "ignore")
                self.poster_image = json_data["Poster"].encode("utf8", "ignore")
                self.imdb_votes = json_data["imdbVotes"].encode("utf8", "ignore")
</code></pre>

<p>Is it recommended to store the data returned as a dictionary as opposed to creating a class for each movie type? </p>

<pre><code>class Movie(object):
""" Class provides a structure to store Movie information """



def __init__(self, imdb_id, title = None, release_year = None, rating = None, run_time = None, genre = None, director = None, actors = None, plot = None, awards = None, poster_image = None, imdb_votes = None, youtube_trailer = None):
    self.imdb_id = imdb_id
    self.title = title
    self.release_year = release_year
    self.rating = rating
    self.run_time = run_time
    self.genre = genre
    self.director = director
    self.actors = actors
    self.plot = plot
    self.awards = awards
    self.poster_image = poster_image
    self.imdb_votes = imdb_votes
    self.youtube_trailer = youtube_trailer


def get_api_data(self):
    """
        Method retreves and parses information for each movie based on imdb_id
    """
        #URL for OMDBI
    url = "http://www.omdbapi.com/?i="+self.imdb_id+"&amp;plot=full&amp;r=json&amp;v=1"
    try:
        response = urllib.urlopen(url)

    except URLERROR as e:
        if hasattr(e, 'reason'):
            print ("Unable to reach a server.")
            print 'Reason: ', e.reason
        elif hasattr(e, 'code'):
            print "Server is unable to fulfill the request."
            print 'Error Code: ', e.code

    # if urllib.urlopen() succeeds, the code jumps to here
    json_data = json.loads(response.read())
    self.imdb_id = json_data["imdbID"].encode("utf8","ignore")
    self.title = json_data["Title"].encode("utf8","ignore")
    self.release_year = int(json_data["Released"].split("")[-1])
    self.rating = json_data["imdbRating"].encode("utf8", "ignore")
    self.run_time = json_data["Runtime"].encode("utf8", "ignore")
    self.genre = json_data["Rated"].encode("utf8", "ignore")
    self.director = json_data["Director"].encode("utf8", "ignore")
    self.actors = json_data["Actors"].encode("utf8", "ignore")
    self.plot = json_data["Plot"].encode("utf8", "ignore")
    self.awards = json_data["Awards"].encode("utf8", "ignore")
    self.poster_image = json_data["Poster"].encode("utf8", "ignore")
    self.imdb_votes = json_data["imdbVotes"].encode("utf8", "ignore")
</code></pre>

python python-2.7