1)
File: ex2.py
import json
list1 = [ "item1" , "item2" , "item3" ]
dict1 = { "key1" : "value1" , "key2" : "value2" , "key3" : list1 }
Convert the dictionary object to Json string and print it out. Load the Json string into a Python object and print out the Python object.
2)
What is the output of the following code ?
import json
class Person:
def __init__( self, name, job=None, pay=0 ): # Normal function args
self.name = name
self.job = job
self.pay = pay
p1 = Person( "John" , "Eng", 100 )
jsonString1 = json.dumps( p1 )
print( jsonString1 )
1)
import json
list1 = [ "item1" , "item2" , "item3" ]
dict1 = { "key1" : "value1" , "key2" : "value2" , "key3" : list1 }
jsonString1 = json.dumps( dict1 )
#We are printing a json string out.
#Now we load this back up to initialize a new dictionary object
print( jsonString1 )
print( "--------------------------------------------" )
dict2 = json.loads( jsonString1 )
print( dict2 )
2)
We cannot convert a custom class object to Json in Python.