creating multiple python class instance from json

1234

I am writing some tests to evaluate a rest service my response is

[ { "Title_Id": 1, "Title": "Mr", "TitleDescription": "Mr", "TitleGender": "Male", "Update_Date": "2012-07-21T18:43:04" }, { "Title_Id": 2, "Title": "Mrs", "TitleDescription": "Mrs", "TitleGender": "Female", "Update_Date": "2012-07-21T18:42:59" }, { "Title_Id": 3, "Title": "Sir", "TitleDescription": "Sir", "TitleGender": "Male", "Update_Date": null } ]

and need to create multiple instance of the class

class TitleInfo:
  def __init__(self, Title_Id, Title, TitleDescription, TitleGender, Update_Date ):
    self.Title_Id = Title_Id
    self.Title = Title
    self.TitleDescription = TitleDescription
    self.TitleGender = TitleGender
    self.Update_Date = Update_Date

what I have done is

def GetTitle(self):
  try:
    response = *#......"The string shown above"*
    if  isinstance(response, str) :
      Records = json.loads(response)
      RecTitles = []
      for num in range(0, len(Records)):
        RecTitle =TitleInfo(Records[num]['Title_Id'],Records[num]['Title'],Records[num]['TitleDescription'],Records[num]['TitleGender'],Records[num]['Update_Date'])
        RecTitles.append(RecTitle)

This is working fine ....I need to know is there more short and sweet way to do that?

niemmi

You could just unpack each dict and give that as an argument to TitleInfo:

RecTitles = [TitleInfo(**x) for x in json.loads(response)]

Here's the explanation from Python tutorial:

In the same fashion, dictionaries can deliver keyword arguments with the **-operator:

>>> def parrot(voltage, state='a stiff', action='voom'):
...     print("-- This parrot wouldn't", action, end=' ')
...     print("if you put", voltage, "volts through it.", end=' ')
...     print("E's", state, "!")
...
>>> d = {"voltage": "four million", "state": "bleedin' demised", "action": "VOOM"}
>>> parrot(**d)
-- This parrot wouldn't VOOM if you put four million volts through it. E's bleedin' demised !

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Java creating instance of ArrayList from other class

From Java

Creating class instance properties from a dictionary?

From Java

Creating new instance from Class with constructor parameter

From

Creating an instance of class

From Dev

Creating class instance from dictionary?

From Dev

Creating instance of nested class

From Dev

Creating an object from every instance of class and content within that element

From Dev

Creating class instance with IHubContext

From Dev

Creating a Class in C# from JSON File

From Dev

Is it possible to handle unknown properties when creating a TS class instance from JSON?

From Dev

Creating multiple class objects with a for loop in python

From Dev

Creating multiple buttons in tkinter in a class - python 3

From Dev

Trouble Creating Instance of Class

From Dev

Creating multiple tcp socket handlers from one class in Python

From Dev

creating multiple instances of a class in Python

From Dev

Creating a new instance/object from a class with nested JSON key value pairs in Ruby

From Dev

Creating a class instance from Linq query result

From Dev

Creating a multiple class instances from user input

From Dev

Problems in creating an instance from a .class file

From Dev

Creating multiple instance of a view

From Dev

Python: Creating a new instance of itself from a base class (possibly via reflection?)

From Dev

Creating an instance of a Javascript class from TypeScript

From Dev

Creating JSON object from Typescript class

From Dev

Creating python object from json

From Dev

Creating JSON from multiple dataframes python

From Dev

Creating instance of child class from parent class based on some input

From Dev

Python logging is not creating multiple instance of log files

From Dev

Calling an instance method without creating an instance of class in Python

From Dev

Creating instance of custom class

Related Related

  1. 1

    Java creating instance of ArrayList from other class

  2. 2

    Creating class instance properties from a dictionary?

  3. 3

    Creating new instance from Class with constructor parameter

  4. 4

    Creating an instance of class

  5. 5

    Creating class instance from dictionary?

  6. 6

    Creating instance of nested class

  7. 7

    Creating an object from every instance of class and content within that element

  8. 8

    Creating class instance with IHubContext

  9. 9

    Creating a Class in C# from JSON File

  10. 10

    Is it possible to handle unknown properties when creating a TS class instance from JSON?

  11. 11

    Creating multiple class objects with a for loop in python

  12. 12

    Creating multiple buttons in tkinter in a class - python 3

  13. 13

    Trouble Creating Instance of Class

  14. 14

    Creating multiple tcp socket handlers from one class in Python

  15. 15

    creating multiple instances of a class in Python

  16. 16

    Creating a new instance/object from a class with nested JSON key value pairs in Ruby

  17. 17

    Creating a class instance from Linq query result

  18. 18

    Creating a multiple class instances from user input

  19. 19

    Problems in creating an instance from a .class file

  20. 20

    Creating multiple instance of a view

  21. 21

    Python: Creating a new instance of itself from a base class (possibly via reflection?)

  22. 22

    Creating an instance of a Javascript class from TypeScript

  23. 23

    Creating JSON object from Typescript class

  24. 24

    Creating python object from json

  25. 25

    Creating JSON from multiple dataframes python

  26. 26

    Creating instance of child class from parent class based on some input

  27. 27

    Python logging is not creating multiple instance of log files

  28. 28

    Calling an instance method without creating an instance of class in Python

  29. 29

    Creating instance of custom class

HotTag

Archive