2

我是 JSONObject 的新手,我正在尝试将LinkedHashMap<String, List<Node>>转换为 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 中的元素。

谢谢。

4

3 回答 3

0

您可以使用stream转换LinkedHashMapJsonObject

Node-类(例如):

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-方法转换值(列表)=>将节点映射到构建器并使用自定义收集器(Collector.of(...))将它们收集到“项目” 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()
                    )
            );
}

StreamMap.Entry<String, List<Node>>每个转换EntryJsonObject并收集所有到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
                    )
            );
于 2019-04-29T12:47:36.527 回答
0

我结合 JSONArray 和 JSONObject 类解决了它。

我使用循环为所有节点创建了主对象:

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();
于 2019-04-30T15:19:56.120 回答
-1

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>
于 2019-04-29T11:11:32.107 回答