logo

Konverter streng til JSON i Python

Inden vi tager et dybt dyk ned i emnet, så lad os få et blik på, hvad strenge er, og hvad er JSON?

Strenge: er en sekvens af tegn, der er angivet ved hjælp af omvendte kommaer ''. De er uforanderlige, hvilket betyder, at de ikke kan ændres, når de først er deklareret.

JSON: står for 'JavaScript Object Notation', JSON-filerne består af tekst, der let kan læses af mennesker og er til stede i form af attribut-værdi-par.

Udvidelsen af ​​JSON-filer er '.json'

Lad os se på den første tilgang til at konvertere en streng til json i Python.

Følgende program illustrerer det samme.

generel beskyttelsesfejl
 # converting string to json import json # initialize the json object i_string = {'C_code': 1, 'C++_code' : 26, 'Java_code' : 17, 'Python_code' : 28} # printing initial json i_string = json.dumps(i_string) print ('The declared dictionary is ', i_string) print ('It's type is ', type(i_string)) # converting string to json res_dictionary = json.loads(i_string) # printing the final result print ('The resultant dictionary is ', str(res_dictionary)) print ('The type of resultant dictionary is', type(res_dictionary)) 

Produktion:

 The declared dictionary is {'C_code': 1, 'C++_code' : 26, 'Java_code' : 17, 'Python_code' : 28} It's type is The resultant dictionary is {'C_code': 1, 'C++_code' : 26, 'Java_code' : 17, 'Python_code' : 28} The type of resultant dictionary is 

Forklaring:

Det er tid til at se forklaringen, så vores logik bliver klar-

  1. Da målet her er at konvertere en streng til json-fil, vil vi først importere json-modulet.
  2. Det næste trin er at initialisere json-objektet, hvori vi har emnenavnet som nøgler, og derefter angives deres tilsvarende værdier.
  3. Herefter har vi brugt dumper() at konvertere et Python-objekt til en json-streng.
  4. Til sidst vil vi bruge indlæser() at parse en JSON-streng og konvertere den til en ordbog.

Brug af eval()

 # converting string to json import json # initialize the json object i_string = ''' {'C_code': 1, 'C++_code' : 26, 'Java_code' : 17, 'Python_code' : 28} ''' # printing initial json print ('The declared dictionary is ', i_string) print ('Its type is ', type(i_string)) # converting string to json res_dictionary = eval(i_string) # printing the final result print ('The resultant dictionary is ', str(res_dictionary)) print ('The type of resultant dictionary is ', type(res_dictionary)) 

Produktion:

 The declared dictionary is {'C_code': 1, 'C++_code' : 26, 'Java_code' : 17, 'Python_code' : 28} Its type is The resultant dictionary is {'C_code': 1, 'C++_code': 26, 'Java_code': 17, 'Python_code': 28} The type of resultant dictionary is 

Forklaring:

hashset java

Lad os forstå, hvad vi har gjort i ovenstående program.

  1. Da målet her er at konvertere en streng til json-fil, vil vi først importere json-modulet.
  2. Det næste trin er at initialisere json-objektet, hvori vi har emnenavnet som nøgler, og derefter angives deres tilsvarende værdier.
  3. Herefter har vi brugt eval() at konvertere en Python-streng til json.
  4. Når programmet udføres, viser det det ønskede output.

Henter værdier

Til sidst vil vi i det sidste program hente værdierne efter konverteringen af ​​streng til json.

Lad os tage et kig på det.

 import json i_dict = '{'C_code': 1, 'C++_code' : 26, 'Java_code':17, 'Python_code':28}' res = json.loads(i_dict) print(res['C_code']) print(res['Java_code']) 

Produktion:

 1 17 

Vi kan observere følgende ting i output-

  1. Vi har konverteret strengen til json ved hjælp af json.loads().
  2. Efter dette har vi brugt tasterne 'C_code' & 'Java_code' til at hente deres tilsvarende værdier.

Konklusion

I denne vejledning lærte vi, hvordan man konverterer en streng til json ved hjælp af Python.