データストア-JSONファイル処理
7170 ワード
概要
JSON(JavaScript Object Notation,JSオブジェクトタグ)は、軽量レベルのデータ交換フォーマットです.ECMAScript(欧州コンピュータ協会が制定したjs仕様)のサブセットに基づいて、プログラミング言語とは完全に独立したテキストフォーマットを用いてデータを格納し、表す.簡潔で明確な階層はJSONを理想的なデータ交換言語にした.読み取りと作成が容易であり、同時に機械の解析と生成が容易であり、ネットワーク伝送効率を効果的に向上させる.
JSONはデータフォーマットをサポートする
辞書とリスト回転JSON
import json
# python json
person = [
{
'username':" ",
'age':18,
'country':'china'
},
{
'username':"root",
'age':15,
'country':'china'
}
]
json_str=json.dumps(person)
print(type(json))
print(type(json_str))
print(json_str)
#
with open('person.json','w',encoding='utf-8') as fp:
# json
json.dump(person,fp,ensure_ascii=False)
JSON文字列loadをPythonオブジェクトにする
import json
person = [
{
'username':" ",
'age':18,
'country':'china'
},
{
'username':"root",
'age':15,
'country':'china'
}
]
json_str=json.dumps(person)
print(type(json_str))
persons = json.loads(json_str)
print(type(persons))
# python
with open('person.json','r',encoding='utf-8') as fp :
persons = json.load(fp)
print(type(persons))
print(persons)
一般的なフォーマット解析Webサイト:https://www.json.cn/