無料 · 開発
JSON から Kotlin データクラス.
JSON サンプルを kotlinx.serialization 用の Kotlin データクラスに変換。@SerialName と null 許容の任意フィールドに対応。
Loading the calculator…
JSON から Kotlin のデータクラスを生成する
- API のレスポンスやサンプルファイルの JSON オブジェクト、またはオブジェクトの配列を貼り付けます。
- ルートクラスの名前を付けます。
- コードをコピーするか、kotlinx.serialization 向けの Models.kt をダウンロードします。
例:キー user_name は @SerialName("user_name") val userName: String になり、1 件の注文にしかないクーポンは String? = null になります。
型の対応
- 整数は Long、小数は Double、リストは List<T> になります。型が混在する値は JsonElement になります。
- camelCase でないキーは @SerialName で元の名前を保つので、JSON をそのままデシリアライズできます。
- 一部の要素にないフィールドや、サンプルで null のフィールドは、既定値 null の null 許容型になります。Kotlin の予約語はエスケープされます。
- 型はサンプルだけから推論するので、完全な例を入れてください。処理はすべてブラウザー内です。iOS には JSON を Swift に変換 を、Java には JSON を Java の record に変換 を使ってください。
制限と対応形式
入力内容はお使いの端末で処理されます。
英語の技術説明
Types are inferred from the sample only. Everything is calculated in your browser; nothing you type is uploaded.
Integers map to Long, decimals to Double, lists to List<T> and varying values to JsonElement. Kotlin keywords are escaped with backticks.
user_name becomes @SerialName("user_name") val userName: String; a coupon present in only one order becomes String? = null.
よくある質問
Gson や Moshi でも使えますか?
アノテーションは kotlinx.serialization 向けです。ほかのライブラリでは @SerialName をそのライブラリの名前用アノテーションに置き換えてください。
あるフィールドが JsonElement 型になるのはなぜですか?
サンプル内でそのフィールドの型が異なるため、1 つの Kotlin 型では表せないからです。
JSON はアップロードされますか?
いいえ。クラスはブラウザー内で生成されます。