json.dump()
时间: 2023-10-21 22:09:39 浏览: 40
The `json.dump()` method is used to serialize Python objects (such as dictionaries, lists, and tuples) into a JSON formatted string and write it to a file-like object. The syntax for using `json.dump()` is as follows:
```python
import json
with open('file.json', 'w') as f:
json.dump(obj, f)
```
Here, `obj` is the Python object you want to serialize and write to a file, and `f` is the file-like object to write the serialized JSON data to. The `with` statement is used to ensure that the file is properly closed after the data has been written.
Note that `json.dump()` will only serialize objects that are supported by JSON (e.g. strings, numbers, lists, and dictionaries). If you need to serialize more complex objects, you may need to write a custom serializer.