如何将 LinkedHashMap<String, List<Node>> 转换为

我是 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 中的元素。


大话西游666
浏览 207回答 3
3回答

绝地无双

我结合 JSONArray 和 JSONObject 类解决了它。我使用循环为所有节点创建了主对象:for&nbsp;(Node&nbsp;node&nbsp;:&nbsp;nodeList){ &nbsp;&nbsp;try &nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JSONObject&nbsp;obj&nbsp;=&nbsp;new&nbsp;JSONObject(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;obj.put("value",&nbsp;node.getValue()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;obj.put("label",&nbsp;node.getLabel()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jsonArrayOne.put(obj) &nbsp;&nbsp;} &nbsp;&nbsp;catch&nbsp;(JSONException&nbsp;e) &nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.info("JSONException"); &nbsp;&nbsp;}}然后将 jsonArrayOne 放入一个 jsonObject 中:jsonObjOne.put("items",&nbsp;jsonArrayOne);并将这个 jsonObjOne 放入一个 jsonArray 中:jsonArrayTwo.put(jsonObjOne);把这个 jsonArrayTwo 放在一个 jsonObject 中:jsonObjTwo.put(element,&nbsp;jsonArrayTwo);最后把这个jsonObjTwo放到jsonArrayFinal中。jsonArrayFinal.put(jsonObjTwo);最后,我将 jsonArrayFinal 转换为字符串:jsonArrayFinal.toString();

千巷猫影

您可以使用stream转换LinkedHashMap为JsonObject:Node-类(例如):public class Node {&nbsp; &nbsp; private final String value;&nbsp; &nbsp; private final String label;&nbsp; &nbsp; private Node(String value, String label) {&nbsp; &nbsp; &nbsp; &nbsp; this.value = value;&nbsp; &nbsp; &nbsp; &nbsp; this.label = label;&nbsp; &nbsp; }&nbsp; &nbsp; //Getters}toItems-方法转换值(列表)=> 将节点映射到构建器并使用自定义收集器(Collector.of(...))将它们收集到“项目” JsonObject:static JsonObject toItems(List<Node> nodes) {&nbsp; &nbsp; return nodes&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(node ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Json.createObjectBuilder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .add("value", node.getValue())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .add("label", node.getLabel())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;).collect(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Collector.of(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Json::createArrayBuilder,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JsonArrayBuilder::add,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JsonArrayBuilder::addAll,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonArrayBuilder ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Json.createObjectBuilder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .add("items", jsonArrayBuilder)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );}Stream将Map.Entry<String, List<Node>>每个转换Entry为JsonObject并收集所有到Root -object:&nbsp;Map<String, List<Node>> nodes = ...&nbsp;JsonObject jo = nodes&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .entrySet()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map((e) -> Json.createObjectBuilder().add(e.getKey(), toItems(e.getValue()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ).collect(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Collector.of(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Json::createObjectBuilder,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JsonObjectBuilder::addAll,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JsonObjectBuilder::addAll,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JsonObjectBuilder::build&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );

Qyouu

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>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java