Python | Möglichkeiten zum Konvertieren von Zeichenfolgen in JSON-Objekte

In diesem Artikel werden wir verschiedene Möglichkeiten zum Konvertieren von Zeichenfolgen in JSON in Python sehen. Dieser Vorgang wird als Serialisierung bezeichnet. Das JSON-Modul bietet Funktionen zum Codieren (Serialisieren) von Python-Objekten JSON Zeichenfolgen und Dekodierung (Deserialisierung) von JSON-Zeichenfolgen Python-Objekte .

  • Codierung (Serialisierung) von JSON : Wenn Sie ein Python-Objekt haben und es in einen JSON-String konvertieren möchten, können Sie das verwenden json.dumps() Funktion. Es verwendet das Python-Objekt als Eingabe und gibt einen JSON-String zurück.
  • Dekodierung (Deserialisierung) von JSON : Wenn Sie eine JSON-Zeichenfolge haben und diese in ein Python-Objekt konvertieren möchten, können Sie die verwenden json.loads() Funktion. Es verwendet den JSON-String als Eingabe und gibt ein Python-Objekt zurück.

Konvertieren Sie einen String in ein JSON-Objekt in Python

Nachfolgend finden Sie Methoden zum Konvertieren von Python-Strings in JSON-Objekte:

  • Benutzen json.loads() Methode
  • Verwendung der eval()-Methode
  • Verwendung der Methode ast.literal_eval()

Konvertieren Sie String in JSON mit der Methode json.loads() in Python

In diesem Beispiel konvertieren wir ein Wörterbuch mithilfe von in ein JSON-Objekt json.dump() Funktion. Anschließend konvertieren wir die Zeichenfolgendarstellung eines JSON-Objekts mithilfe von in ein Wörterbuch json.loads() Methode.

Python3




import> json> # initialising json object> ini_string> => {> 'nikhil'> :> 1> ,> 'akash'> :> 5> ,> > 'manjeet'> :> 10> ,> 'akshat'> :> 15> }> # printing initial json> ini_string> => json.dumps(ini_string)> print> (> 'initial 1st dictionary'> , ini_string)> print> (> 'type of ini_object'> ,> type> (ini_string))> # converting string to json> final_dictionary> => json.loads(ini_string)> # printing final result> print> (> 'final dictionary'> ,> str> (final_dictionary))> print> (> 'type of final_dictionary'> ,> type> (final_dictionary))>

Ausgabe:

initial 1st dictionary {'manjeet': 10, 'nikhil': 1, 'akshat': 15, 'akash': 5} type of ini_object final dictionary {'nikhil': 1, 'manjeet': 10, 'akshat': 15, 'akash': 5} type of final_dictionary 

Konvertieren Sie String mit der Methode eval() in Python in JSON

Die Funktion eval() in Python wertet die Ausdruckseingabe als Python-Ausdruck aus und führt den Python-Ausdruck (Code) innerhalb des Programms aus.

Beispiel

In diesem Beispiel konvertieren wir Python Zeichenfolge Darstellung in einem Wörterbuch mithilfe von eval()-Methode .

Python3




# initialising json object string> ini_string> => '''{'nikhil': 1, 'akash' : 5,> > 'manjeet' : 10, 'akshat' : 15}'''> # printing initial json> print> (> 'initial 1st dictionary'> , ini_string)> print> (> 'type of ini_object'> ,> type> (ini_string))> # converting string to json> final_dictionary> => eval> (ini_string)> # printing final result> print> (> 'final dictionary'> ,> str> (final_dictionary))> print> (> 'type of final_dictionary'> ,> type> (final_dictionary))>

Ausgabe:

initial 1st dictionary {'nikhil': 1, 'akash' : 5, 'manjeet' : 10, 'akshat' : 15} type of ini_object final dictionary {'nikhil': 1, 'manjeet': 10, 'akash': 5, 'akshat': 15} type of final_dictionary 

Konvertieren Sie String mit der Methode ast.literal_eval() in JSON

Die Methode ast.literal_eval() ist Teil des ast-Moduls (abstrakter Syntaxbaum) in Python. Diese Funktion wertet einen Ausdrucksknoten aus, eine Zeichenfolge, die aus einem Literal besteht, und konvertiert ihn in ein Python-Wörterbuchobjekt.

Beispiel

Hier verwenden wir die Methode ast.literal_eval() des letzten Moduls, um eine String-Darstellung des Wörterbuchs in ein Python-Format zu konvertieren Wörterbuch . Die Funktion ast.literal_eval analysiert die Zeichenfolge und erstellt ein Python-Wörterbuchobjekt mit denselben Eigenschaften und Werten. Das resultierende Wörterbuchobjekt hat den Typ Diktat. Wir können dann mithilfe der Standard-Wörterbuchzugriffsnotation (z. B. dict_obj[name]) auf die Eigenschaften des Wörterbuchobjekts zugreifen.

Python3




import> ast> # initialize the string to be converted> string> => '{'name': 'John', 'age': 30, 'city': 'New York'}'> # use the ast.literal_eval function to parse the string and create a dictionary object> dict_obj> => ast.literal_eval(string)> # printing final result> print> (> 'Initial string dictionary: '> ,string)> print> (> 'Final dictionary: '> ,dict_obj)> print> (> type> (dict_obj))>

Ausgabe:

Initial string dictionary: {'name': 'John', 'age': 30, 'city': 'New York'} Final dictionary: {'name': 'John', 'age': 30, 'city': 'New York'} 


Das Könnte Ihnen Gefallen