java - 如何将 Map 转换为 javax.json.JsonObject?
问题描述
我可以这样做:
Map<String, String> mapA = ...;
Map<String, String> mapB = mapA.entrySet().stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue
));
但是当我试图这样做时:
... mapA.entrySet().stream()
.collect(JsonCollectors.toJsonObject(
JsonObject.Entry::getKey,
JsonObject.Entry::getValue
));
我明白了
不能从静态上下文中引用非静态方法
对于JsonObject.Entry::getKey, JsonObject.Entry::getValue
部分。
为什么?
解决方案
您可以使用JsonObjectBuilder 的 add 方法:
JsonObjectBuilder builder = Json.createObjectBuilder();
mapA.forEach(builder::add);
JsonObject obj = builder.build();
推荐阅读
- c# - Instagram 提及 API - 无法在 DEV 模式下检索媒体详细信息
- javascript - 在JS中分组数组键
- php - 调用未定义的方法codeigniter异常
- c# - 如何在 Xamarin.Forms 的 Picker 中的 ItemDisplayBinding 中显示默认枚举值
- ios12 - iOS 12 及更高版本,获取 iPhone 连接的 Wifi SSID
- unity3d - 如何在 Unity 3d 中使用 Microsoft Lync 2013/SharePoint?
- css - Django应用样式来更新文件字段
- xamarin.android - Xamarin AccountKit 中的二进制 XML 错误膨胀类
- solr - Solr 的 TimeZone 行为对于 EST 和 PST 不同
- jenkins - Jenkins no initialAdminPassword:没有这样的文件或目录