logo

Python の json.dumps()

JSON JavaScript Object Notation の頭字語です。その名前にもかかわらず、JSON は言語に依存しない形式であり、システム間でデータを送信したり、場合によってはデータを保存したりするために最も一般的に使用されます。 Python や他の多くのプログラミング言語で書かれたプログラムは、JSON 形式のデータを取り込み、メモリ内のデータを JSON 形式にシリアル化できます。 パイソン json と呼ばれる組み込みパッケージを通じて JSON をサポートします。この機能を使用するには、データをシリアル化または逆シリアル化する Python スクリプトまたはモジュールに json パッケージをインポートします。 JSON では、二重引用符で囲まれ、コロンで区切られたコンマ区切りのキーと値のペアが使用されます。 JSON ファイルの本文は、中括弧 { } または角括弧 [] (一部のロケールでは括弧とも呼ばれます) で区切ることができます。 JSON 形式は Python の辞書に似ているように見えますが、JSON 形式の詳細には大きな違いがあるため、両方の形式を使用する場合は注意してください。

intを文字列Javaに変換する方法

注記: 詳細については、「読む」を参照してください。 Python を使用して JSON を作成および解析する



Json.dumps()

json.dumps() 関数は、Python オブジェクトのサブセットを json 文字列に変換します。すべてのオブジェクトが変換可能であるわけではないため、JSON にシリアル化する前に公開したいデータのディクショナリを作成する必要がある場合があります。

構文:
json.dumps(obj, *,skipkeys=False,ensure_ascii=True,check_circular=True,allow_nan=True,cls=None,indent=None,separators=None,default=None,sort_keys=False,**kw)
パラメーター:
オブジェクト: obj を JSON 形式のストリームとしてシリアル化する
スキップキー: Skipkeys が True (デフォルト: False) の場合、基本型 (str、int、float、bool、None) ではない辞書キーは、TypeError を発生させる代わりにスキップされます。
ensure_ascii: ensure_ascii が True (デフォルト) の場合、出力ではすべての受信非 ASCII 文字がエスケープされることが保証されます。 ensure_ascii が False の場合、これらの文字はそのまま出力されます。
check_circular: check_circular が False (デフォルト: True) の場合、コンテナー タイプの循環参照チェックはスキップされ、循環参照により OverflowError (またはそれ以上のエラー) が発生します。
許可入力: allow_nan が False (デフォルト: True) の場合、JSON 仕様に厳密に従って範囲外の float 値 (nan、inf、-inf) をシリアル化すると、ValueError になります。 allow_nan が True の場合、それに相当する JavaScript (NaN、Infinity、-Infinity) が使用されます。
インデント: indent が負でない整数または文字列の場合、JSON 配列要素とオブジェクト メンバーはそのインデント レベルで整形して表示されます。インデント レベルが 0、負、または改行のみを挿入する場合。 [なし] (デフォルト) では、最もコンパクトな表現が選択されます。正の整数のインデントを使用すると、レベルごとにその数のスペースがインデントされます。 indent が文字列 ( など) の場合、その文字列は各レベルのインデントに使用されます。
区切り文字: 指定する場合、区切り文字は (item_separator, key_separator) タプルである必要があります。デフォルトは、インデントが None の場合は (‘, ‘, ‘: ‘)、それ以外の場合は (‘, ‘, ‘: ‘) です。最もコンパクトな JSON 表現を取得するには、(‘, ‘, ‘:’) を指定して空白を削除する必要があります。
デフォルト: 指定した場合、デフォルトは、シリアル化できないオブジェクトに対して呼び出される関数である必要があります。オブジェクトの JSON エンコード可能なバージョンを返すか、TypeError を発生させる必要があります。指定しない場合、TypeError が発生します。
ソートキー: sort_keys が True (デフォルト: False) の場合、辞書の出力はキーによって並べ替えられます。

例 #1: Python 辞書を json.dumps() 関数に渡すと、文字列が返されます。



Python3






import> json> # Creating a dictionary> Dictionary>=>{>1>:>'Welcome'>,>2>:>'to'>,> >3>:>'Geeks'>,>4>:>'for'>,> >5>:>'Geeks'>}> > # Converts input dictionary into> # string and stores it in json_string> json_string>=> json.dumps(Dictionary)> print>(>'Equivalent json string of input dictionary:'>,> >json_string)> print>(>' '>)> # Checking type of object> # returned by json.dumps> print>(>type>(json_string))>

>

>

出力

辞書の同等の JSON 文字列: {1: Welcome、2: to、3: Geeks、4: for、5: Geeks}

例2: スキップキーを True (デフォルト: False) に設定すると、基本タイプではないキーが自動的にスキップされます。

Python3




import> json> Dictionary>=>{(>1>,>2>,>3>):>'Welcome'>,>2>:>'to'>,> >3>:>'Geeks'>,>4>:>'for'>,> >5>:>'Geeks'>}> # Our dictionary contains tuple> # as key, so it is automatically> # skipped If we have not set> # skipkeys = True then the code> # throws the error> json_string>=> json.dumps(Dictionary,> >skipkeys>=> True>)> print>(>'Equivalent json string of dictionary:'>,> >json_string)>

>

>

出力

辞書の同等の JSON 文字列: {2: to, 3: Geeks, 4: for, 5: Geeks}

例 #3:

Python3




import> json> # We are adding nan values> # (out of range float values)> # in dictionary> Dictionary>=>{(>1>,>2>,>3>):>'Welcome'>,>2>:>'to'>,> >3>:>'Geeks'>,>4>:>'for'>,> >5>:>'Geeks'>,>6>:>float>(>'nan'>)}> # If we hadn't set allow_nan to> # true we would have got> # ValueError: Out of range float> # values are not JSON compliant> json_string>=> json.dumps(Dictionary,> >skipkeys>=> True>,> >allow_nan>=> True>)> print>(>'Equivalent json string of dictionary:'>,> >json_string)>

>

>

出力:

Javaは文字を文字列にキャストします

辞書の同等の JSON 文字列: {2: to、3: Geeks、4: for、5: Geeks、6: NaN}

例 #4:

Python3




import> json> Dictionary>=>{(>1>,>2>,>3>):>'Welcome'>,>2>:>'to'>,> >3>:>'Geeks'>,>4>:>'for'>,> >5>:>'Geeks'>,>6>:>float>(>'nan'>)}> # Indentation can be used> # for pretty-printing> json_string>=> json.dumps(Dictionary,> >skipkeys>=> True>,> >allow_nan>=> True>,> >indent>=> 6>)> print>(>'Equivalent json string of dictionary:'>,> >json_string)>

>

>

出力:

Equivalent json string of dictionary: { '2': 'to', '3': 'Geeks', '4': 'for', '5': 'Geeks', '6': NaN }>

例5:

マイクロリシックカーネル

Python3




import> json> Dictionary>=>{(>1>,>2>,>3>):>'Welcome'>,>2>:>'to'>,> >3>:>'Geeks'>,>4>:>'for'>,> >5>:>'Geeks'>,>6>:>float>(>'nan'>)}> # If specified, separators should be> # an (item_separator, key_separator)tuple> # Items are separated by '.' and key,> # values are separated by '='> json_string>=> json.dumps(Dictionary,> >skipkeys>=> True>,> >allow_nan>=> True>,> >indent>=> 6>,> >separators>=>(>'. '>,>' = '>))> print>(>'Equivalent json string of dictionary:'>,> >json_string)>

>

>

出力:

Equivalent json string of dictionary: { '2' = 'to'. '3' = 'Geeks'. '4' = 'for'. '5' = 'Geeks'. '6' = NaN }>

例6:

Python3




import> json> Dictionary>=>{>'c'>:>'Welcome'>,>'b'>:>'to'>,> >'a'>:>'Geeks'>}> json_string>=> json.dumps(Dictionary,> >indent>=> 6>,> >separators>=>(>'. '>,>' = '>),> >sort_keys>=> True>)> print>(>'Equivalent json string of dictionary:'>,> >json_string)>

>

>

出力:

Equivalent json string of dictionary: { 'a' = 'Geeks'. 'b' = 'to'. 'c' = 'Welcome' }>