Python JSON decode和encode

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()


你可能感兴趣的:(Python)