JSON signifie JavaScript Object Notation. Cela signifie qu'un fichier de script (exécutable), composé de texte dans un langage de programmation, est utilisé pour stocker et transférer les données. Python prend en charge JSON via un package intégré appelé JSON. Pour utiliser cette fonctionnalité, nous importons le PythonJSON package dans un script Python. Le texte en JSON est rédigé via une chaîne entre guillemets qui contient une valeur dans le mappage clé-valeur dans { }. C'est similaire au dictionnaire de Python .
Fonction utilisée
json.load() : json.load() La fonction est présente dans le module 'JSON' intégré à Python. Cette fonction est utilisée pour analyser la chaîne JSON.
json.loads() : json.loads() La fonction est présente dans le module 'json' intégré à Python. Cette fonction est utilisée pour analyser la chaîne JSON.
Convertir une chaîne JSON en dictionnaire Python
Dans cet exemple, nous allons convertir une chaîne JSON en dictionnaire Python à l'aide de la méthode json.loads() du module JSON en Python. Tout d'abord, nous importons le module json, puis définissons la chaîne JSON, puis convertissons la chaîne JSON en dictionnaire Python en la passant à json.loads() en paramètre. Nous avons imprimé le dictionnaire et leurs valeurs en utilisant les clés comme indiqué dans le résultat.
Python3
# Import JSON module> import> json> # Define JSON string> jsonString> => '{ 'id': 121, 'name': 'Naveen', 'course': 'MERN Stack'}'> # Convert JSON String to Python> student_details> => json.loads(jsonString)> # Print Dictionary> print> (student_details)> # Print values using keys> print> (student_details[> 'name'> ])> print> (student_details[> 'course'> ])> |
>
>Sortir
{'id': 121, 'name': 'Naveen', 'course': 'MERN Stack'} Naveen MERN Stack>
Convertir un fichier JSON en objet Python
Vous trouverez ci-dessous le fichier JSON que nous allons convertir en dictionnaire Python en utilisant json.load() Hommes
Dans le code ci-dessous, nous ouvrons d’abord le fichier data.json en utilisant la gestion des fichiers en Python, puis convertissez le fichier en objet Python à l'aide de la méthode json.load(), nous devons également imprimer le type de données après la conversion et imprimer le dictionnaire.
Python3
tutoriel java pour les débutants
# Python program to demonstrate> # Conversion of JSON data to> # dictionary> # importing the module> import> json> # Opening JSON file> with> open> (> 'data.json'> ) as json_file:> > data> => json.load(json_file)> > # Print the type of data variable> > print> (> 'Type:'> ,> type> (data))> > # Print the data of dictionary> > print> (> '
People1:'> , data[> 'people1'> ])> > print> (> '
People2:'> , data[> 'people2'> ])> |
>
>
Sortir :
Convertir un objet JSON imbriqué en dictionnaire
Dans cet exemple, nous allons convertir le JSON imbriqué en un dictionnaire Python. Pour les données JSON, nous utiliserons le même fichier JSON que celui utilisé dans l'exemple ci-dessus.
Python3
# importing the module> import> json> # Opening JSON file> with> open> (> 'data.json'> ) as json_file:> > data> => json.load(json_file)> > # for reading nested data [0] represents> > # the index value of the list> > print> (data[> 'people1'> ][> 0> ])> > > # for printing the key-value pair of> > # nested dictionary for loop can be used> > print> (> '
Printing nested dictionary as a key-value pair
'> )> > for> i> in> data[> 'people1'> ]:> > print> (> 'Name:'> , i[> 'name'> ])> > print> (> 'Website:'> , i[> 'website'> ])> > print> (> 'From:'> , i[> 'from'> ])> > print> ()> |
>
>
Sortir :
Convertir une chaîne JSON en dictionnaire en Python
Dans cet exemple, nous allons convertir la chaîne json en dictionnaire Python à l'aide de la méthode json.loads(). Tout d’abord, nous importerons le module JSON. Créez une chaîne json et stockez-la dans une variable « json_string ». Après cela, nous convertirons la chaîne json en dictionnaire en passant « json_string » dans json.loads() comme argument et stockerons le dictionnaire converti dans « json_dict ». Enfin, imprimez le dictionnaire Python.
Python3
import> json> # JSON string> json_string> => '{'Name': 'Suezen', 'age': 23, 'Course': 'DSA'}'> # Convert JSON string to dictionary> json_dict> => json.loads(json_string)> print> (json_dict)> |
>
constructeur de chaînes Java
>Sortir
{'Name': 'Suezen', 'age': 23, 'Course': 'DSA'}>