Python中字典和JSON互轉操作例項

2022-10-04 23:33:24 字數 959 閱讀 8582

json是一種輕量級的資料交換格式,各種語言都有良好的支援。字典是python的一種資料結構。可以看成關聯陣列。

有些時候我們需www.cppcns.com要設計到字典轉換成json序列化到檔案,或者從檔案中讀取json。簡單備忘一下。

dict轉json寫入檔案

復www.cppcns.com制** **如下:

#!/usr/bin/env python

# coding=utf-8

import json

d =

json.dump(d, open('/tmp/result.txt', 'w'))

寫入結果

複製** **如下:

cat /tmp/result.txt

讀取json

複製** **如下:

#!/usr/bin/env python

# coding=utf-8

import json

d = json.load(open('/tmp/result.txt','r'))

print d, type(d)

執行結果

複製** **如下:

&mjozhwlt;type 'dict'>

其他ps:關於json操作,這裡再為大家推薦幾款比較實用的json**工具供大家參考使用:

**json**檢驗、檢驗、美化、格式化工具:

json**格式化工具:

format

**xml/json互相轉換工具:

json****格式化/美化/壓縮/編輯/轉換工具:

codeformat

**json壓縮/轉義工具:

_yasuo_trans

c語言風格/html/css/json**格式化美化工具:

本文標題: python中字典和json互轉操作例項

本文位址: /jiaoben/python/118612.html

python中string和bytes互轉

首先來設定乙個原始的字串,python 3.2.3 default,apr 11 2012,07 15 24 msc v.1500 32bit intel on win32 type help credits or license for more information.website type ...

python 字典 列表和json物件互轉

import unittest,time,os import requests,json from public.log import log,logger def better output json str return json.dumps json.loads json str indent...

python 有 字典 無 json

首先需要說明的是 python中沒有json資料型別!所以在python中無論是單引號括起來的資料還是雙引號括起來的資料,型別都是dict!python中有dict的資料型別!python中的資料型別有 int str float list bool tuple dict set 字典是一種資料結構...