python jsonにおけるdump、dumps、load、loadsの使い方と違いは?


"""
Python3 JSON     
     https://blog.csdn.net/kassadinsw/article/details/73912645
    :https://docs.python.org/3/library/json.html
"""

import json

#            (Python      ),  Python           json         !   !   !
data_obj = {
    "   ": {
        "   ": ["   ", "  ", "  "],
        "   ": ["   ", "   ", "   "],
        "   ": ["   ", "   ", "   "],
    },
    "   ": {
        "   ": [],
        "   ": [],
        "   ": [],
    }
}

# ---------------------------------------------------   ------------------------------------------------------------


"""
dumps:       
sort_keys:  key  
indent: 4     ,       
ensure_ascii:       ascii (   )

"""
s_dumps = json.dumps(data_obj, sort_keys=True, indent=4, ensure_ascii=False)
print(s_dumps)

# ---------------------------------------------------   ------------------------------------------------------------


"""
dump:            
dump()              ,             
          UTF-8    

*                  data.json  ,         json             

"""
with open("data.json", "w", encoding="UTF-8") as f_dump:
    s_dump = json.dump(data_obj, f_dump, ensure_ascii=False)
print(s_dump)

# ---------------------------------------------------   ------------------------------------------------------------


"""
load:              
         

"""
with open("data.json", "r", encoding="UTF-8") as f_load:
    r_load = json.load(f_load)
print(r_load)

# ---------------------------------------------------   ------------------------------------------------------------


"""
loads:          

"""
r_loads = json.loads(s_dumps)
print(r_loads)

arg = '{"bakend": "www.oldboy.org", "record": {"server": "100.1.7.9", "weight": 20, "maxconn": 30}}'

a = json.loads(input('        :'),encoding='utf-8')
print(a)