How to dynamically build a JSON object with Python?

后端 未结 5 1781
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 18:04

I am new to Python and I am playing with JSON data. I would like to dynamically build a JSON object by adding some key-value to an existing JSON object.

I tried the

5条回答
  •  南笙
    南笙 (楼主)
    2020-11-28 18:23

    All previous answers are correct, here is one more and easy way to do it. For example, create a Dict data structure to serialize and deserialize an object

    (Notice None is Null in python and I'm intentionally using this to demonstrate how you can store null and convert it to json null)

    import json
    print('serialization')
    myDictObj = { "name":"John", "age":30, "car":None }
    ##convert object to json
    serialized= json.dumps(myDictObj, sort_keys=True, indent=3)
    print(serialized)
    ## now we are gonna convert json to object
    deserialization=json.loads(serialized)
    print(deserialization)
    

提交回复
热议问题