如何将 LinkedHashMap<String, List<Node>> 转换为 JSONObject?
How to convert a LinkedHashMap<String, List<Node>> into a JSONObject?
我是 JSONObject 的新手,我正在尝试将 LinkedHashMap> 转换为 json 文件。
地图结构如下:
"element_one", {Node1, Node2, etc...}
"element_two", {Node1, Node2, etc...}
每个节点都有两个属性:值和标签。
我想要的是这样的:
{
"element_one": [
{
"items": [
{
"value": "1",
"label": "Donald"
},
{
"value": "2",
"label": "Goofy"
}]
}],
"element_two": [
{
"items": [
{
"value": "1",
"label": "Peter"
},
{
"value": "2",
"label": "Wendy"
}]
}}
我正在使用 JSONObject,我在不同的线程中寻找解决方案,但没有任何帮助。我知道这是一个特殊的问题,但我需要用这个数据结构得到这个结果。
难点是在地图中循环节点元素而不覆盖已经插入到 JSONObject 中的元素。
谢谢。
GSON 库将帮助您将对象转换为 JSON。
Gson gson = new Gson();
String json = gson.toJson(myMap,LinkedHashMap.class);
Maven 依赖
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
您可以使用stream
将LinkedHashMap
转换为JsonObject
:
Node
-class(举例):
public class Node {
private final String value;
private final String label;
private Node(String value, String label) {
this.value = value;
this.label = label;
}
//Getters
}
toItems
-method convert value(List) => 将节点映射到构建器并使用自定义收集器(Collector.of(...)
)收集它们到"items" JsonObject
:
static JsonObject toItems(List<Node> nodes) {
return nodes
.stream()
.map(node ->
Json.createObjectBuilder()
.add("value", node.getValue())
.add("label", node.getLabel())
).collect(
Collector.of(
Json::createArrayBuilder,
JsonArrayBuilder::add,
JsonArrayBuilder::addAll,
jsonArrayBuilder ->
Json.createObjectBuilder()
.add("items", jsonArrayBuilder)
.build()
)
);
}
Map.Entry<String, List<Node>>
的 Stream
将每个 Entry
转换为 JsonObject
并全部收集到 Root-object:
Map<String, List<Node>> nodes = ...
JsonObject jo = nodes
.entrySet()
.stream()
.map((e) -> Json.createObjectBuilder().add(e.getKey(), toItems(e.getValue()))
).collect(
Collector.of(
Json::createObjectBuilder,
JsonObjectBuilder::addAll,
JsonObjectBuilder::addAll,
JsonObjectBuilder::build
)
);
我结合 JSONArray 和 JSONObject 解决了它 class。
我已经为所有节点使用循环创建了主要对象:
for (Node node : nodeList)
{
try
{
JSONObject obj = new JSONObject();
obj.put("value", node.getValue());
obj.put("label", node.getLabel());
jsonArrayOne.put(obj)
}
catch (JSONException e)
{
log.info("JSONException");
}
}
然后把jsonArrayOne放到一个jsonObject中:
jsonObjOne.put("items", jsonArrayOne);
并将这个 jsonObjOne 放入一个 jsonArray 中:
jsonArrayTwo.put(jsonObjOne);
将此 jsonArrayTwo 放入 jsonObject 中:
jsonObjTwo.put(element, jsonArrayTwo);
最后把这个jsonObjTwo放到jsonArrayFinal中。
jsonArrayFinal.put(jsonObjTwo);
最后我将 jsonArrayFinal 转换成一个字符串:
jsonArrayFinal.toString();
我是 JSONObject 的新手,我正在尝试将 LinkedHashMap
"element_one", {Node1, Node2, etc...}
"element_two", {Node1, Node2, etc...}
每个节点都有两个属性:值和标签。
我想要的是这样的:
{
"element_one": [
{
"items": [
{
"value": "1",
"label": "Donald"
},
{
"value": "2",
"label": "Goofy"
}]
}],
"element_two": [
{
"items": [
{
"value": "1",
"label": "Peter"
},
{
"value": "2",
"label": "Wendy"
}]
}}
我正在使用 JSONObject,我在不同的线程中寻找解决方案,但没有任何帮助。我知道这是一个特殊的问题,但我需要用这个数据结构得到这个结果。
难点是在地图中循环节点元素而不覆盖已经插入到 JSONObject 中的元素。
谢谢。
GSON 库将帮助您将对象转换为 JSON。
Gson gson = new Gson();
String json = gson.toJson(myMap,LinkedHashMap.class);
Maven 依赖
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
您可以使用stream
将LinkedHashMap
转换为JsonObject
:
Node
-class(举例):
public class Node {
private final String value;
private final String label;
private Node(String value, String label) {
this.value = value;
this.label = label;
}
//Getters
}
toItems
-method convert value(List) => 将节点映射到构建器并使用自定义收集器(Collector.of(...)
)收集它们到"items" JsonObject
:
static JsonObject toItems(List<Node> nodes) {
return nodes
.stream()
.map(node ->
Json.createObjectBuilder()
.add("value", node.getValue())
.add("label", node.getLabel())
).collect(
Collector.of(
Json::createArrayBuilder,
JsonArrayBuilder::add,
JsonArrayBuilder::addAll,
jsonArrayBuilder ->
Json.createObjectBuilder()
.add("items", jsonArrayBuilder)
.build()
)
);
}
Map.Entry<String, List<Node>>
的 Stream
将每个 Entry
转换为 JsonObject
并全部收集到 Root-object:
Map<String, List<Node>> nodes = ...
JsonObject jo = nodes
.entrySet()
.stream()
.map((e) -> Json.createObjectBuilder().add(e.getKey(), toItems(e.getValue()))
).collect(
Collector.of(
Json::createObjectBuilder,
JsonObjectBuilder::addAll,
JsonObjectBuilder::addAll,
JsonObjectBuilder::build
)
);
我结合 JSONArray 和 JSONObject 解决了它 class。
我已经为所有节点使用循环创建了主要对象:
for (Node node : nodeList)
{
try
{
JSONObject obj = new JSONObject();
obj.put("value", node.getValue());
obj.put("label", node.getLabel());
jsonArrayOne.put(obj)
}
catch (JSONException e)
{
log.info("JSONException");
}
}
然后把jsonArrayOne放到一个jsonObject中:
jsonObjOne.put("items", jsonArrayOne);
并将这个 jsonObjOne 放入一个 jsonArray 中:
jsonArrayTwo.put(jsonObjOne);
将此 jsonArrayTwo 放入 jsonObject 中:
jsonObjTwo.put(element, jsonArrayTwo);
最后把这个jsonObjTwo放到jsonArrayFinal中。
jsonArrayFinal.put(jsonObjTwo);
最后我将 jsonArrayFinal 转换成一个字符串:
jsonArrayFinal.toString();