Spring Mvc在树视图中返回原始Json

时间:2015-06-23 11:41:05

标签: java json spring

treeController.java

@Controller
public class TreeController {

    @RequestMapping(value="/mytree",method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {
        return "tree";
    }
    @RequestMapping(value = "/getTechList", method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
     public @ResponseBody List<FlatObject.Technology> getTechList(){
        FlatObject ensureCapacity = new FlatObject();
        List<FlatObject.Technology> techList = ensureCapacity.getTechList();
         return techList;


        } 

    }

生成treenode的模型类

public class FlatObject {

    List<Technology> techList = new ArrayList<Technology>(1000);
    private boolean child;
    private Technology root;

    public List<Technology> getTechList() {
        if(techList.isEmpty()){      
            ((ArrayList<Technology>) techList).ensureCapacity(1000);
             techList.add(new Technology("Root",1,0));
            techList.add(new Technology("Node 1",2,1));
            techList.add(new Technology("Node 1.1",3,2));
            techList.add(new Technology("Node 1.2",4,2));

            techList.add(new Technology("Node 2",5,1));
            techList.add(new Technology("Node 2.1",6,5));
            techList.add(new Technology("Node 2.2",7,5));
            techList.add(new Technology("Node 2.3",8,5));
        }
        return techList;
    }


    public void setTechList(List<Technology> techList) {
        this.techList = techList;
    }

    public class Technology {

        private int id;
        private int Parentid ;
        private String techName;

        public int getid() {
            return id;
        }

        public void setid(int id) {
            this.id = id;
        }

        public int getParentid() {
            return Parentid;
        }

        public void setParentid(int Parentid) {
            this.Parentid = Parentid;
        }

        public String getTechName() {
            return techName;
        }

        public void setTechName(String techName) {
            this.techName = techName;
        }

        public Technology(String techName,int id, int Parentid) {
            this.id = id;
            this.Parentid = Parentid;
            this.techName = techName;
        }
    }
}

输出:

{
id: 1,
techName: "Root",
parentid: 0
},
{
id: 2,
techName: "Node 1",
parentid: 1
},
{
id: 3,
techName: "Node 1.1",
parentid: 2
},
{
id: 4,
techName: "Node 1.2",
parentid: 2
},
{
id: 5,
techName: "Node 2",
parentid: 1
},
{
id: 6,
techName: "Node 2.1",
parentid: 5
},

我正在使用java和springmvc创建树结构并返回到json对象中的视图.json不是树格式.so 我希望json格式像不同级别的节点,这些都在同一级别。如何做到这一点,请帮助

我想要这样的输出:

{
data: "Node 3",
state: "open",
children: [
{
data: "Node 3.1",
attr: {
techname: "Node 3.1"
}
},
{
data: "Node 3.2",
attr: {
techname: "Node 3.2"
}
}
],
attr: {
techname: "Node 3"
}
},
{
data: "Node 4",
state: "open",
children: [
{
data: "Node 4.2",
attr: {
techname: "Node 4.2"
}
},
{
data: "Node 4.3",
attr: {
techname: "Node 4.3"
}
},
{
data: "Node 4.4",
attr: {
techname: "Node 4.4"
}
}
],
attr: {
techname: "Node 4"
}
},

我正在使用HashMap和list来生成像Json这样的输出我粘贴但是它不起作用,请帮助如何实现这一点,必须在Spring控制器端编写代码

@Controller
public class HomeController {

    @RequestMapping(value = "/mytree", method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {
        return "tree";
    }

    @RequestMapping(value = "/getTechList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody Map<String, String> getTTechList() {

        try {
              DataClass dataClass = new DataClass();
              List<DataClass.Technology> listCPV = dataClass.getTechList();

            Map<String, String> map1 = new HashMap<String, String>();
            ObjectMapper mapper = new ObjectMapper();
            for (int i = 0; i < listCPV.size();) {
                Map<String,String> map2 = new HashMap<String, String>();

                map2.put("state", "open");
                map2.put("data", listCPV.get(i).getTechName());


                Map<String, String> map3 = new HashMap<String, String>();
                map3.put("techname", listCPV.get(i).getTechName());
                String jsonAttr = null;
                map2.put("attr", jsonAttr);
                map3 = null;

                if (listCPV.get(i + 1).getId() == 0) {
                    Map<String, String> map4 = new HashMap<String, String>();

                    while (listCPV.get(i + 1).getId() == 0) {
                        i++;
                        Map<String, String> map5 = new HashMap<String, String>();
                        map5.put("data", listCPV.get(i).getTechName());

                        Map<String, String> map6 = new HashMap<String, String>();
                        map6.put("techname", listCPV.get(i).getTechName());
                        String jsonChildAttr = null;
                        map5.put("attr",jsonChildAttr);
                        map6 = null;

                        map4.putAll(map5);
                        map5 = null;

                        if (listCPV.size() == (i + 1)) {
                            break;
                        }
                    }


                }
                i++;
            }


        } catch (Exception e) {
            System.out.println(e);
        }
        return null;



    }
}

2 个答案:

答案 0 :(得分:0)

我认为你需要这样的东西

{
id: 1,
techName: "Root",
parentid: 0,
child: [{
        id: 2,
        techName: "Node 1",
        parentid: 1,
        child:[ {
            id: 3,
            techName: "Node 1.1",
            parentid: 2,
            child:
            },{
            id: 4,
            techName: "Node 1.2",
            parentid: 2,
            child:
            } ]
        },{
        id: 5,
        techName: "Node 2",
        parentid: 1,
        child: [{
            id: 6,
            techName: "Node 2.1",
            parentid: 5,
            child:
            }]
        }]
}

如果是,则必须对您的java代码进行更改。从我的exp开始,它应该是列表和地图的组合。 如果您仍然无法做到,请告诉我,但要分享您为实现同样目的所做的努力。

答案 1 :(得分:0)

也许它会有所帮助

public class FlatObject {

    private int parentId;
    private String techName;
    List<Technology> childList = new ArrayList<Technology>(1000);

    public FlatObject() {
        this.parentId = 1;
        this.techName = "Root";
        if(this.childList.isEmpty()){
            ((ArrayList<Technology>) this.childList).ensureCapacity(1000);
            this.childList.add(new Technology("Node 1",2,1));
            this.childList.add(new Technology("Node 1.1",3,2));
            this.childList.add(new Technology("Node 1.2",4,2));

            this.childList.add(new Technology("Node 2",5,1));
            this.childList.add(new Technology("Node 2.1",6,5));
            this.childList.add(new Technology("Node 2.2",7,5));
            this.childList.add(new Technology("Node 2.3",8,5));
        }
    }

public @ResponseBody List<FlatObject> getTechList() {
FlatObject ensureCapacity = new FlatObject();
        FlatObject ensureCapacity1 = new FlatObject();
        List<FlatObject> techList = new ArrayList<>();
        techList.add(ensureCapacity);
        techList.add(ensureCapacity1);
        return techList;
}

[
  {
    "techName": "Root",
    "parentId": 1,
    "childList": [
      {
        "id": 2,
        "parentid": 1,
        "techName": "Node 1"
      },
      {
        "id": 3,
        "parentid": 2,
        "techName": "Node 1.1"
      },
      {
        "id": 4,
        "parentid": 2,
        "techName": "Node 1.2"
      },
      {
        "id": 5,
        "parentid": 1,
        "techName": "Node 2"
      },
      {
        "id": 6,
        "parentid": 5,
        "techName": "Node 2.1"
      },
      {
        "id": 7,
        "parentid": 5,
        "techName": "Node 2.2"
      },
      {
        "id": 8,
        "parentid": 5,
        "techName": "Node 2.3"
      }
    ]
  },
  {
    "techName": "Root",
    "parentId": 1,
    "childList": [
      {
        "id": 2,
        "parentid": 1,
        "techName": "Node 1"
      },
      {
        "id": 3,
        "parentid": 2,
        "techName": "Node 1.1"
      },
      {
        "id": 4,
        "parentid": 2,
        "techName": "Node 1.2"
      },
      {
        "id": 5,
        "parentid": 1,
        "techName": "Node 2"
      },
      {
        "id": 6,
        "parentid": 5,
        "techName": "Node 2.1"
      },
      {
        "id": 7,
        "parentid": 5,
        "techName": "Node 2.2"
      },
      {
        "id": 8,
        "parentid": 5,
        "techName": "Node 2.3"
      }
    ]
  }
]