How to dynamically build a JSON object with Python?

后端 未结 5 1775
没有蜡笔的小新
没有蜡笔的小新 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:37

    You build the object before encoding it to a JSON string:

    import json
    
    data = {}
    data['key'] = 'value'
    json_data = json.dumps(data)
    

    JSON is a serialization format, textual data representing a structure. It is not, itself, that structure.

提交回复
热议问题