Python JSON decode和encode

来源:互联网 发布:军事武器数据库 编辑:程序博客网 时间:2024/05/16 19:44

Python: Decode和Encode JSON

import json, dumps encode数据,loads decode数据。

def DecodingJson(json_file):    dic = {}    jfile = open(json_file)    while True:        line = jfile.readline()        if len(line) == 0:            break        decode_file = json.loads(line)        for key, value in decode_file.items():            dic[key] = value    jfile.close()    return dic def EncodingJson(filename, data):    out_json_file = open(filename,"w")    json_data = json.dumps(data, sort_keys = True)     out_json_file.truncate()    out_json_file.write(json_data)    out_json_file.close()


0 0
原创粉丝点击