Outils pour utilisateurs

Outils du site


python:json

Table des matières

JSON

data = json.load(f, …) Décodage depuis un fichier
data = json.loads(s, …) Décodage depuis une chaîne
json.dump(f, data, …) Encodage vers un fichier
json.dumps(data, …) Encodage vers une chaîne

Encodage

import json
 
data = [
     'foo',
     {
         'bar': ('baz', None, 1.0, 2)
     }
]
 
j = json.dumps(data)

Avec indentation :

j = json.dumps(data, indent=4)
print(j)

Vers un fichier :

with open('data.txt', 'w') as f:
    json.dump(data, f, indent=4)

Décodage

Depuis une chaîne :

import json
 
data = json.loads(str)

Depuis un fichier :

import json
 
with open("/home/marc/fichier.json", 'r') as f:
    data = json.load(f)
python/json.txt · Dernière modification: 2021/10/20 08:25 par marclebrun