亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將 LinkedHashMap<String, List<Node>> 轉換為

如何將 LinkedHashMap<String, List<Node>> 轉換為

大話西游666 2022-10-26 15:57:11
我是 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 中的元素。
查看完整描述

3 回答

?
絕地無雙

TA貢獻1946條經驗 獲得超4個贊

我結合 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();


查看完整回答
反對 回復 2022-10-26
?
千巷貓影

TA貢獻1829條經驗 獲得超7個贊

您可以使用stream轉換LinkedHashMap為JsonObject:


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()

                    )

            );

}

Stream將Map.Entry<String, List<Node>>每個轉換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

                    )

            );


查看完整回答
反對 回復 2022-10-26
?
Qyouu

TA貢獻1786條經驗 獲得超11個贊

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>


查看完整回答
反對 回復 2022-10-26
  • 3 回答
  • 0 關注
  • 171 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號