canat4702
canat4702

Reputation: 13

Creating JSON in a class in Python

I need to create a JSON class base called JCarOwner. It has to get attributes from its owner such as name, manufacture and year. I want to make it an unknown attribute:

def add_co (existing owner name, new_attribute , attribute content)

But I don't really know how. This is my code (corrected):

import json

class JCarOwner:

    CarOwner = []

    def __init__(self,name,manufacture=None,production_year=None):
        super(JCarOwner, self).__init__()
        self.CarOwner.append(json.dumps({"Owner_Name": name, "Car_Manufacture": manufacture, "Production_Year": production_year}))

    def COsave(filename):
        JCarOwner.CarOwner.dumps(filename,separators=(',', ':'))

    def COload(filename):
        JCarOwner.CarOwner.append(json.load(filename))

    def DispInfo(Car_Owner):

        for owner in JCarOwner.CarOwner:

            CO = json.load(owner)
            if CO[owner]["Owner_Name"]==Car_Owner:
                print(CO[owner])


jane = JCarOwner("Jane","Mazda",2016)
bob = JCarOwner("bob")     

JCarOwner.DispInfo(jane)

error:

AttributeError: 'str' object has no attribute 'read'

Upvotes: 1

Views: 225

Answers (1)

hspandher
hspandher

Reputation: 16753

Use self.CarOwner instead of plain CarOwner.

 def __init__(self,name,manufacture,production_year):
   super(JCarOwner, self).__init__()

   self.CarOwner.append(json.dumps({"Owner_Name": name, "Car_Manufacture": manufacture, "Production_Year": production_year}))

Please try to follow certain python conventions.

Upvotes: 1

Related Questions