0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

キーが日本語のjsonをjacksonで読む

0
Posted at
static record 注文(String 合計金額, ユーザ お客様) {}
static record ユーザ(String 氏名) {}

JsonMapper json = new JsonMapper();
注文 表示用注文 = json.readValue("""
  {
    "合計金額": "1,234円",
    "お客様": {
      "氏名": "サンプル太郎"
    }
  }
  """, 注文.class);
System.out.println(表示用注文);
注文[合計金額=1,234円, お客様=ユーザ[氏名=サンプル太郎]]

twitterで「キーも値も日本語のjsonをそのまま画面表示する」という文化(?)が一部にはあるとかなんとかいうのを見かけて、じゃあJavaというかjacksonだとどうなんだろ、と試したところフツーに出来る、というただそれだけの話。

個人的にはJavaで変数等に日本語使うのはOK派。そうでなくても、テストデータをjsonで定義する場合に日本人向けには日本語キーは分かりやすい場合が多いかもなぁ……などと思ったり思わなかったりした。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?