python and json conversion

Import JSON 

DEF python_to_json ():
     "" " convert python object JSON " "" 
    D = {
         ' name ' : ' python Books ' 

    } 
    REST = json.dumps (D, = indent. 4 )
     Print (REST) 

DEF json_to_python ( ):
     "" " " to convert json Python "" " 
    Data = '' ' 
        { 
        " name ":" the Python Book ", 
        " origin_price ": 66, 
        " pub_date ":"2018-4-15 17:00:00",2018-4-15 17:00:00",
        "store":["jingdong","taobao"],
        "author":["zhangshan","lisi","jhone"],
        "is_valid":true,
        "is_sale":false,
        "meta":{
            "isbn":"abc-123",
            "pages":300
            },
        "desc":null
        }
    '''
    rest = json.loads(data)
    print(rest)
    print(rest['name'])

def json_to_python_from_file():
    """"从文件读取内容并转换成python文件"""
    f = open('./venv/static_/book.json','r',encoding='utf-8')#只读模式
    s = f.read()
    print(s)
    rest = json.loads(s)
    print(rest['name'])

    f.close()

if __name__=='__main__':
    python_to_json()
    # json_to_python()
    #json_to_python_from_file()

 

Guess you like

Origin www.cnblogs.com/zsjlovewm/p/11106231.html