使用GSON将多个Collection <String>合并为一个JSON字符串

nim*_*rod 1 java collections json gson

有没有办法将不同的String-Collections合并为一个JSON'String'?我想要一个看起来像这样的JSON字符串:

{"vendor":[Sun, HP, IBM],"product":[bla, bla, bla],"availability":[high, mid, low],"capacity":[bla, bla, bla], ...}
Run Code Online (Sandbox Code Playgroud)

这是我的Java代码的一部分:

Collection<String> vendor = bh.getAllVendors();
Collection<String> product = bh.getProductsForVendor(vendor);
Collection<String> availability = bh.getAllAvailabilities();
Collection<String> capacity = bh.getCapacityForVendor(vendor);
Collection<String> signaling = bh.getSignalingForVendor(vendor);
Collection<String> backup = bh.getBackupForVendor(vendor);

Gson gson = new Gson();
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激.

jmr*_*ruc 7

如果将它们添加到地图中,这将是最简单的:

    Gson gson = new Gson();

    Map<String, Collection<String>> map = new HashMap<>();
    map.put("vendor", vendor);
    map.put("product", product);
    //etc

    System.out.println(gson.toJson(map));
Run Code Online (Sandbox Code Playgroud)

产生 {"product":["bla","bla","bla"],"vendor":["Sun","IBM","HP"]}