> For the complete documentation index, see [llms.txt](https://legacy.cookielau.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://legacy.cookielau.com/archives/3-python/2-library/3-jsonnotes.md).

# Json Notes

## Json.load(s)

`load` 读入需要**文件流**

```python
import json
with open('test.json', 'r') as f:
    js_file = json.load(f)
```

`loads` 读入需要**字符串**

```python
import json
js_file = json.loads("{"1":"first", "2":"second", "3":"third"}")
```

## Json.dumps

`dumps`为json格式的导出，以下列出了几个常见的参数：

```python
with open("courses.json", "w", encoding='utf-8') as f:
    f.write(str(json.dumps(total_list, indent=4, ensure_ascii=False)))
```

其中打开文件时`encoding='utf-8'`和dumps时`ensure_ascii=False`为保证导出的json中的**中文格式编码正常**。\
其中`indent=4`是设置json格式的缩紧空格个数。
