0

我创建了一个 json,它有一个根节点和几个使用 java 的子节点现在我要求根下的子节点也可能有一些子节点。但我无法做到这一点。这就是我所做的远的....

class Entry {

    private String name;

    public String getChildren() {
        return name;
    }

    public void setChildren(String name) {
        this.name = name;
    }
}

public class JsonApplication {

    public static void main(String[] args) {
    // TODO code application logic here

        String arr[] = {"Culture", "Salary", "Work", "Effort"};
        EntryListContainer entryListContainer = new EntryListContainer();
        List<Entry> entryList1 = new ArrayList<>();
        List<Entry> entryList2 = new ArrayList<>();
        for (int i = 0; i < arr.length; i++) {
            Entry entry1 = new Entry();
            entry1.setChildren(arr[i]);
            entryList1.add(entry1);
            entryList2.add(entry1);

            /*Child nodes are created here and put into entryListContainer*/
            entryListContainer.setEntryList1(entryList1);
            entryListContainer.setEntryList1(entryList2);
        }


        /*Root node this will collapse and get back to Original position on click*/

        entryListContainer.setName("Employee");     
        entryListContainer.setName("Culture");  
        Map<String, String> mapping = new HashMap<>();
        mapping.put("entryList1", "name");

        Gson gson = new GsonBuilder().serializeNulls().setFieldNamingStrategy(new DynamicFieldNamingStrategy(mapping)).create();
        System.out.println(gson.toJson(entryListContainer));
    }
}

class DynamicFieldNamingStrategy implements FieldNamingStrategy {

    private Map<String, String> mapping;

    public DynamicFieldNamingStrategy(Map<String, String> mapping) {
        this.mapping = mapping;
    }

    @Override
    public String translateName(Field field) {
       String newName = mapping.get(field.getName());
       if (newName != null) {
           return newName;
       }

       return field.getName();
    }
}

class EntryListContainer {

    private List<Entry> children;
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setEntryList1(List<Entry> entryList1) {
        this.children = entryList1;
    }

    public List<Entry> getEntryList1() {
        return children;
    }
}

这是我得到的 json 输出

{
  "children": [
    {
      "name":"Culture"
    },
    {
      "name":"Salary"
    },
    {
      "name":"Work"
    },
    {
      "name":"Effort"
    }
  ],
  "name":"Employee"
}

但是我需要

{
  "name":"Culture",
  "children":[
    {
      "name":"Culture"
    },
    {
      "name":"Salary"
    },
    {
      "name":"Work"
    },
    {
      "name":"Effort"
    }
  ],
  "name":"Work",
  "children" : [
    {
     "name":"Culture"
    },
    {
      "name":"Work"
    }
  ]
}
4

1 回答 1

1

我对你的代码有点困惑,但我很清楚:你想得到什么。因此,我从头开始创建了一些代码,您可以复制并运行以查看如何获得所需的 JSON。

可能元素的顺序对您很重要(请注意,在 JSON 对象中,键的顺序并不重要 - 是地图!-),所以我编辑了一些不是纯 Gson 做事方式的代码,但它完全创建了您的示例.

package stackoverflow.questions;

import java.lang.reflect.Field;
import java.util.*;

import com.google.gson.*;

public class JsonApplication {

   public static class EntryListContainer {

      public List<Entry> children = new ArrayList<Entry>();
      public Entry name;

   }

   public static class Entry {

      private String name;

      public Entry(String name) {
         this.name = name;
      }

   }

   public static void main(String[] args) {

      EntryListContainer elc1 = new EntryListContainer();
      elc1.name = new Entry("Culture");
      elc1.children.add(new Entry("Salary"));
      elc1.children.add(new Entry("Work"));
      elc1.children.add(new Entry("Effort"));

      EntryListContainer elc2 = new EntryListContainer();
      elc2.name = new Entry("Work");
      elc2.children.add(new Entry("Culture"));
      elc2.children.add(new Entry("Work"));

      ArrayList<EntryListContainer> al = new ArrayList<EntryListContainer>();
      Gson g = new Gson();

      al.add(elc1);
      al.add(elc2);

      StringBuilder sb = new StringBuilder("{");
      for (EntryListContainer elc : al) {

         sb.append(g.toJson(elc.name).replace("{", "").replace("}", ""));
         sb.append(",");
         sb.append(g.toJson(elc.children));
         sb.append(",");
      }

      String partialJson = sb.toString();

      if (al.size() > 1) {
         int c = partialJson.lastIndexOf(",");
         partialJson = partialJson.substring(0, c);
      }

      String finalJson = partialJson + "}";
      System.out.println(finalJson);

   }
}

这是执行:

{"name":"Culture",[{"name":"Salary"},{"name":"Work"},{"name":"Effort"}],"name":"Work",[{"name":"Culture"},{"name":"Work"}]}
于 2013-11-16T00:05:18.607 回答