How to fill an object from a class with data from JSON data?

Zenast :

I want to fill my object with data received from JSON

Data keys from JSON are named the same as the parameters in my class.

is there any efficient way to create fill my object? hence my class will have over 50 variables.

my testing Class:

class Joint:
    spineX = 0.0
    spineY = 0.0
    spineZ = 0.0

Json data:

print(jsonData) #prints -> {"spineX":8.9,"spineY":7.7,"spineZ":9.9}
Justin Ezequiel :

Perhaps add an __init__ to your class:

import json


class Joint:
    def __init__(self, spineX, spineY, spineZ):
        self.spineX = spineX
        self.spineY = spineY
        self.spineZ = spineZ


s = '{"spineX":8.9,"spineY":7.7,"spineZ":9.9}'
o = json.loads(s)
joint = Joint(**o)

print(vars(joint))

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=397580&siteId=1