2

次のような JSON ファイルがあります。

{"id" : "1", "name" : "David"}  // this represent testdata for the class Person
{"accountid" : "1188", "accountnumber" : "119295567"}  // this represent testdata for the class account
{"id" : "22", "date" : "22.11.2013"} // this represent testdata for the class transaction

これで、3 つの Java クラスができました (JSON ファイルと get メソッドと set メソッドのような適切な属性を持ちます)。

  • アカウント
  • 取引

Junit テストを作成し、JSON ファイルを使用します。1 つの JSON ファイルのみを使用して、3 つの異なるオブジェクトを生成します。

Gsonを使用してこれを行うにはどうすればよいですか? これは、オブジェクトを逆シリアル化するためにこれまでに試みたPersonものです。

Gson gson = new GsonBuilder().create();
String jsonTestFile = FileUtils.readFileToString(new File(this.pathForJsonTestFile
         + "testFile.json"));

Person person = gson.fromJson(jsonTestFile,
         Person.class);

しかし、JSON に依存するアカウント オブジェクト、トランザクション オブジェクト、または人物オブジェクトを明示的に作成するにはどうすればよいでしょうか?

4

1 に答える 1