日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:Python之JSON函數(shù)介紹

JSON函數(shù)

創(chuàng)新互聯(lián)建站是一家專注于成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計與策劃設(shè)計,沙坪壩網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專注于網(wǎng)站建設(shè)十余年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:沙坪壩等地區(qū)。沙坪壩做網(wǎng)站價格咨詢:13518219792

使用 JSON 函數(shù)需要導(dǎo)入 json 庫:import json。

舉例說明,如下:

a.json內(nèi)容格式:

{"car":{"price":1100,"color":"red"},"mac":{"price":7999,"color":"black"},"abc":{"price":122,"color":"green"}}

json.load()

import json
with open('a.json') as fp:
    shop_dic = json.load(fp)  #從a.json文件內(nèi)讀取數(shù)據(jù),返回結(jié)果為字典:{'abc': {'price': 122, 'color': 'green'},
     'mac': {'price': 7999, 'color': 'black'}, 'car': {'price': 1100, 'color': 'red'}}
    print(shop_dic)

json.loads()

s_json = '{"name":"niuniu","age":20,"status":true}'
print(json.loads(s_json))         #將json串轉(zhuǎn)換為字典:{'age': 20, 'status': True, 'name': 'niuniu'}

相關(guān)推薦:《python視頻教程》

json.dump()

import json
with open('a.json', 'a+') as fp:
    dic = {'name': 'niuniu', 'age': 18}
    fp.seek(0)
    fp.truncate()
    json.dump(dic, fp)    #將字典轉(zhuǎn)換為json串寫入文件

寫入的a.json如下:

{"age": 18, "name": "niuniu"}
json.dumps()
import json
dic = {'name': 'niuniu', 'age': 18}
print(json.dumps(dic))           #將字典轉(zhuǎn)換為json串:{"name": "niuniu", "age"

擴展小知識點:

將字典內(nèi)容寫入json文件,包含中文。

1. 中文寫入json文件后顯示亂碼,怎么解決?                      ensure_ascii = False

2. 寫入的字典內(nèi)容顯示為不一行,顯示不美觀,怎么解決? indent = 4

import json
d = {"Name": "戰(zhàn)神","sex" : ["男","女","人妖"],"Education":{"GradeSchool" : "第一小學(xué)",
"MiddleSchool" : ["第一初中" , "第一高中"], "University" :{ "Name" : "哈佛大學(xué)", "Specialty" : ["一年級","二年級"]}}}
with open('a.json', 'w', encoding='utf-8') as f:
    # 中文顯示亂碼問題, ensure_ascii = False
    # json格式化問題, indent = 8
    # s = json.dumps(d, ensure_ascii=False, indent=8)  字典轉(zhuǎn)換為json 字符串
    # f.write(s)
    
    #第二種寫法
    json.dump(d, f, ensure_ascii=False,indent=8)

寫入的json文件,a.json:


當(dāng)前名稱:創(chuàng)新互聯(lián)Python教程:Python之JSON函數(shù)介紹
URL標題:http://m.5511xx.com/article/cohdppc.html