[Python常用库]json

import json


# json的接口与pickle非常类似,需要注意的是,json dump/load之后是unicode编码
def test_json():
    # dumps/loads test
    t = {'name': ['a', 'b', '测试']}
    p = json.dumps(t)
    print p

    o = json.loads(p)
    print o

    # dump/load test with file
    with open('json.txt', 'w') as fp:
        json.dump(t, fp)

    with open('json.txt', 'r') as fp:
        print json.load(fp)

原文地址:http://www.isware.cn/python-modules/01-data-persistence-and-exchange/json/

你可能感兴趣的:([Python常用库]json)