日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python json数据格式数组内元素递增赋值_利用Python实现JSON格式数据的编码与解码操作...

發(fā)布時間:2023/12/10 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python json数据格式数组内元素递增赋值_利用Python实现JSON格式数据的编码与解码操作... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

導(dǎo)讀

JSON (全稱:JavaScript Object Notation )是一種輕量級的數(shù)據(jù)交換格式,所謂的輕量級,是與 XML 數(shù)據(jù)結(jié)構(gòu)相比而言的,在描述相同的數(shù)據(jù)下,JSON 所需的字符比XML 的要少,這就意味著相同意義的數(shù)據(jù), JSON 能更快完成傳輸,所需的流量也會減少,所以在移動互聯(lián)網(wǎng)中JSON 數(shù)據(jù)格式會用的更多。

關(guān)于 JSON 數(shù)據(jù)完整概述,可直訪:

JSON 數(shù)據(jù)類型

JSON 是一種非常靈活,具有高度可定制化的數(shù)據(jù)格式,其一般由以下幾種數(shù)據(jù)類型單一或混合組成:

number:整型數(shù)據(jù);

boolean:布爾類型數(shù)據(jù),true或false;

string:字符串數(shù)據(jù);

null:null;

array:數(shù)組(Array),類似 Python 的列表 — [];

object:類似 Python 的字典數(shù)據(jù) — { Object:value }。

其中,對象(object) 和 數(shù)組(array) 為比較特殊且常用的兩種類型:

J?SON 對象

基本格式:

{“Object1”: "value1", “Object2”: "value2", ...}

JSON 對象數(shù)據(jù)格式與 Python字典類似,同樣以鍵值對形式存在,外層使用大括號{}包裹,Object 為“鍵”,value 為對應(yīng)的“值”,“鍵”與“值”之間用冒號:間隔,元素間用逗號,間隔,其中 Object 可用數(shù)字或字符串表示,value 可以是任意類型的數(shù)據(jù),可以是數(shù)字、字符串、字典、數(shù)組等。

示例:

{

"name": "taitai",

"birthday": "2000-1-1",

"hobby": "['basketball', 'football']",

"score": "{'chinese': '88', 'math': '95', 'english': '82'}"

}

JSON 數(shù)組

基本格式:

["element1", "element2", ...]

JSON 數(shù)組與 Python 列表數(shù)據(jù)的格式類似,均是一連串的元素集合,在 JSON 數(shù)組一個較為特殊的數(shù)據(jù)類型,它也可以像對象那樣使用鍵值對,但還是索引使用得多。同樣,其值的類型可以是任意類型。

示例:

[{

"name": "taitai",

"birthday": "2000-1-1",

},

{

"name": "taitai",

"birthday": "2000-1-1",

}

]

JSON 規(guī)定字符集必須為 UTF-8 編碼。同時,為了統(tǒng)一解析,JSON 還規(guī)定 Object (鍵) 必須使用雙引號""包裹,且當 value(值) 為字符串數(shù)據(jù)時,也必須用雙引號""包裹。

JSON 編碼 & 解碼

在互聯(lián)網(wǎng)中,如果需要傳輸對象數(shù)據(jù),就需要先將該對象轉(zhuǎn)化為 JSON 格式的字符串,這個過程稱為“對象序列化(編碼)”,反之,在接收到 JSON 字符串后,要轉(zhuǎn)為目標對象使用時,就需要進行 “反序列化(解碼)”,兩者是互逆過程。

Python 數(shù)據(jù)與 JSON 數(shù)據(jù)映射關(guān)系

Python 數(shù)據(jù)

JSON 數(shù)據(jù)

整數(shù)、浮點數(shù)等數(shù)據(jù)類型

數(shù)字

字符串

字符串

布爾類型(True 、False)

true、false

Null

null

列表、元組

數(shù)組

字典

對象

JSON 編碼

Python 提供了一個內(nèi)置的 json 模塊用于處理 Python數(shù)據(jù) 與 JSON數(shù)據(jù) 的相互轉(zhuǎn)換操作。其中模塊提供的 dumps 和 dump 方法用于將 Python 對象轉(zhuǎn)換為 JSON 數(shù)據(jù),即實現(xiàn)編碼過程。

實例:

# coding=utf-8

import json

# 創(chuàng)建數(shù)據(jù)

data = {

'name': 'taitai',

'age': 18,

'hobby': ['basketball', 'football'],

'score': {'chinese': '88', 'math': '95', 'english': '82'}

}

print(data)

print(type(data)) # 編碼前數(shù)據(jù)類型

# dumps 編碼(單行輸出)

data2 = json.dumps(data)

print(data2)

print(type(data2)) # 編碼后數(shù)據(jù)類型

# dumps 編碼(排版輸出,指定逐層跳格為 4)

data2 = json.dumps(data, indent=4)

print(data2)

# dump方法:將數(shù)據(jù)編碼,并寫入指定的文件

# 不排版

with open('./data2.json', 'w') as f:

json.dump(data2, f)

# 排版

with open('./data3.json', 'w') as f:

json.dump(data2, f, indent=4)

實例輸出:

{'name': 'taitai', 'age': 18, 'hobby': ['basketball', 'football'], 'score': {'chinese': '88', 'math': '95', 'english': '82'}}

{"name": "taitai", "age": 18, "hobby": ["basketball", "football"], "score": {"chinese": "88", "math": "95", "english": "82"}}

{

"name": "taitai",

"age": 18,

"hobby": [

"basketball",

"football"

],

"score": {

"chinese": "88",

"math": "95",

"english": "82"

}

}

同時還會在當前目錄下創(chuàng)建 data2.json 和 data3.json 兩個文件。

JSON 解碼

上述實例實現(xiàn)了 JSON 的編碼操作,前面也說了有編碼就有解碼,這是一個相互伴隨的過程,json 模塊同樣提供了 loads() 和 load() 方法實現(xiàn) JSON 數(shù)據(jù)的解碼操作。

實例:

# coding=utf-8

import json

# 創(chuàng)建數(shù)據(jù)

json_data = r'{"name": "taitai", "age": 18, "hobby": ["basketball", "football"], "score": {"chinese": "88", "math": "95", "english": "82"}}'

print("json_data 數(shù)據(jù):", json_data)

print("json_data 數(shù)據(jù)類型:", type(json_data)) #

# 解碼

data = json.loads(json_data)

print("data 數(shù)據(jù):", data)

print("data 數(shù)據(jù)類型:", type(data)) #

print(data['name'])

print(data['hobby'])

print(data['score'])

# 解碼 JSON 文件

with open('./data2.json', 'r') as f: # 使用前面編碼生成的 JSON 文件

data2 = json.load(f)

print("JSON 文件解碼后,內(nèi)容為:\n", data2)

實例輸出:

json_data 數(shù)據(jù): {"name": "taitai", "age": 18, "hobby": ["basketball", "football"], "score": {"chinese": "88", "math": "95", "english": "82"}}

json_data 數(shù)據(jù)類型:

data 數(shù)據(jù): {'name': 'taitai', 'age': 18, 'hobby': ['basketball', 'football'], 'score': {'chinese': '88', 'math': '95', 'english': '82'}}

data 數(shù)據(jù)類型:

taitai

['basketball', 'football']

{'chinese': '88', 'math': '95', 'english': '82'}

JSON 文件解碼后,內(nèi)容為:

{

"name": "taitai",

"age": 18,

"hobby": [

"basketball",

"football"

],

"score": {

"chinese": "88",

"math": "95",

"english": "82"

}

}

喜歡 (6)or分享 (0)

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的python json数据格式数组内元素递增赋值_利用Python实现JSON格式数据的编码与解码操作...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。