logo

PythonでJSONを辞書に変換する

JSON は JavaScript Object Notation の略です。データの保存と転送には、プログラミング言語のテキストで構成されるスクリプト (実行可能) ファイルが使用されることを意味します。 Python は、JSON と呼ばれる組み込みパッケージを通じて JSON をサポートします。この機能を使用するには、 Python JSON Python スクリプトにパッケージ化します。 JSON 内のテキストは、{ } 内のキーと値のマッピングの値を含む引用符で囲まれた文字列によって行われます。の辞書に似ています パイソン

使用する機能

json.load(): json.load() この関数は、Python の組み込み「JSON」モジュールに存在します。この関数は、JSON 文字列を解析するために使用されます。



json.loads(): json.loads() 関数は Python 組み込みの「json」モジュールに存在します。この関数は、JSON 文字列を解析するために使用されます。

JSON文字列を辞書Pythonに変換する

この例では、Python の JSON モジュールの json.loads() メソッドを使用して、JSON 文字列を Python Dictionary に変換します。まず、jsonモジュールをインポートし、JSON文字列を定義した後、パラメータでjson.loads()に渡すことでJSON文字列をPython辞書に変換します。出力に見られるように、キーを使用して辞書とその値を出力しました。

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'>])>

mysqlで列の型を変更する

>

>

出力

{'id': 121, 'name': 'Naveen', 'course': 'MERN Stack'} Naveen MERN Stack>

JSONファイルをPythonオブジェクトに変換

以下は、使用して Python 辞書に変換する JSON ファイルです。 json.load() 男性

彼らは歌手です

Python-json

以下のコードでは、まず data.json ファイルを開きます。 ファイル処理 Python で実行し、 json.load() メソッドを使用してファイルを Python オブジェクトに変換します。また、変換後のデータのタイプを出力し、辞書を出力します。

Python3




# 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'>])>

Javaでのデザインパターン

>

>

出力:

Python-json

ネストされた JSON オブジェクトを辞書に変換する

この例では、ネストされた JSON を Python 辞書に変換します。 JSON データの場合は、上の例で使用したものと同じ JSON ファイルを使用します。

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>()>

それはです
>

>

出力:

Python-json

Python で JSON 文字列を辞書に変換する

この例では、json.loads() メソッドを使用して、json 文字列を Python 辞書に変換します。まずはJSONモジュールをインポートします。 JSON文字列を作成し、それを変数「json_string」に保存します。その後、「json_string」をjson.loads()に引数として渡すことでJSON文字列を辞書に変換し、変換された辞書を「json_dict」に保存します。最後に、Python 辞書を出力します。

Cプログラミングのサンプルプログラム

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)>

>

>

出力

{'Name': 'Suezen', 'age': 23, 'Course': 'DSA'}>