簡體   English   中英

如何使用 Lombok + Gson 創建 JSON object 陣列?

[英]How do you create an JSON object array using Lombok + Gson?

Description : I am using Lombok's @Builder annotation to "build" a JSON payload, and then gson to convert it to a proper JSON output.

但是如何通過builder()方法構建一個數組呢?

代碼:

        RoleType RoleType = RoleType.getEnumByUserRole("Marketing");
        PropertyBean PropertyBean = ConfigFactory.create(PropertyBean.class);
        String defaultStore = "null";

        //this is the object that's suppose to be an array.
        Group group = Group.builder()
                .groupId(RoleType.getGroupId())
                .changedById(PropertyBean.sssUser())
                .storeCode(defaultStore)
                .primary(false).build();

        String lastName = "QA User";
        int numOfDays = 1;
        String defaultLocale = "en";
        User newUser = User.builder()
                .firstName(RoleType.getAcronym())
                .lastName(lastName)
                .startDay(getCurrentYearMonthDate())
                .endDay(addDaysToYearMonthDate(numOfDays))
                .password(PropertyBean.tempPass())
                .rightHand(true)
                .operatorId("123456")
                .userStores(userGroup) //<--- the userStoes object should be an array.
                .locale(defaultLocale).build();

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String body = gson.toJson(newUser);

        System.out.println(body);


Output:

{
  "firstName": "MKT_AUTO",
  "lastName": "QA User",
  "locale": "en",
  "rightHand": true,
  "startDay": "2020-02-03",
  "endDay": "2020-02-04",
  "operatorId": "123456",
  "password": "Temp-Auto01",
  "global": false,
  "userStores": {
    "storeCode": "null",
    "groupId": 24,
    "changedById": "000000081",
    "primary": false
  },
  "active": false,
  "lockedOutFlag": false
}

所需 Output

{
  "firstName": "MKT_AUTO",
  "lastName": "QA User",
  "locale": "en",
  "rightHand": true,
  "startDay": "2020-02-03",
  "endDay": "2020-02-04",
  "operatorId": "81",
  "password": "Temp-Auto01",
  "global": false,
  "userStores": [{ //<---- Array
    "storeCode": "null",
    "groupId": 24,
    "changedById": "000000081",
    "primary": false
  }],
  "active": false,
  "lockedOutFlag": false
}

省略對列表類型的builder的使用是要走的路。 然后只使用ArrayList<>()方法就可以了。

例子 :

        List<Group> userStores = new ArrayList<>();
        userStores.add(new Group("216", 1, "81", true));

        String lastName = "QA User";
        int numOfDays = 1;
        String defaultLocale = "en";
        User newUser = User.builder()
                .firstName(RoleType.getAcronym())
                .lastName(lastName)
                .startDay(getCurrentYearMonthDate())
                .endDay(addDaysToYearMonthDate(numOfDays))
                .password(PropertyBean.tempPass())
                .rightHand(true)
                .operatorId("123456")
                .userStores(userStores)
                .locale(defaultLocale).build();

輸出 :

{
  "firstName": "MKT_AUTO",
  "lastName": "QA User",
  "locale": "en",
  "rightHand": true,
  "startDay": "2020-02-03",
  "endDay": "2020-02-04",
  "operatorId": "123456",
  "password": "Temp-Auto01",
  "global": false,
  "userStores": [
    {
      "storeCode": "216",
      "groupId": 1,
      "changedById": "81",
      "primary": true
    }
  ],
  "active": false,
  "lockedOutFlag": false
}

我知道這是一個老問題,可能不再有什么不同,但是 Collections 與 Lombok 的構建器語法配合得很好。

import lombok.Builder;

@Builder
public class TestBuilder {

  private final List<String> testStrings;

}

這樣你就可以調用TestBuilder.builder().testStrings(List.of("test", "test2")).build(); 沒有任何問題。

您還可以添加@Singular注釋來實現.add函數,以將奇異 object 添加到數組中。

Baeldung上可以找到一個很好的例子。

由於 OP 沒有顯示他的 lombok 來源,我只能假設用戶 class 並不期望 userStores 的集合,因為發送了一個組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM