1. 程式人生 > >EASYUI 樹形選單 fastjson JAVA巢狀物件轉JSON

EASYUI 樹形選單 fastjson JAVA巢狀物件轉JSON

使用EASYUI 的樹形導航選單 JAVA物件轉選單

我用的是JDK1.8 使用 fastjson1.2.7版本 從github下載下來的原始碼

EasyUiTree.java物件程式碼如下:

package org.bingley.test;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * @Title: EasyUiTree.java
 * @Package org.bingley.test
 * @Description: TODO(用一句話描述該檔案做什麼)
 * @Copyright: Copyright (c) 2014 FJBL All Rights Reserved
 * @author bingley
 * @date 2015年11月11日 下午5:36:18
 * @version V1.00.00
*/
public class EasyUiTree implements Serializable {
 /**
  *
  */
 private static final long serialVersionUID = 3714827586565908167L;
 private String id;
 private String text;
 private String state;
 private String checked;
 private Map<String,String> attributes;
 private List<EasyUiTree> children;
 public String getId() {
  return id;
 }
 public void setId(String id) {
  this.id = id;
 }
 public String getText() {
  return text;
 }
 public void setText(String text) {
  this.text = text;
 }
 public String getState() {
  return state;
 }
 public void setState(String state) {
  this.state = state;
 }
 public String getChecked() {
  return checked;
 }
 public void setChecked(String checked) {
  this.checked = checked;
 }
 public Map<String, String> getAttributes() {
  return attributes;
 }
 public void setAttributes(Map<String, String> attributes) {
  this.attributes = attributes;
 }
 public List<EasyUiTree> getChildren() {
  return children;
 }
 public void setChildren(List<EasyUiTree> children) {
  this.children = children;
 }

 
 
}

測試程式碼如下:

package org.bingley.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;


/**@ClassName: TestJsonTree
 * @Description: TODO(這裡用一句話描述這個類的作用)
 * @Copyright: Copyright (c) 2015 FJBL All Rights Reserved
 * @author bingley
 * @date 2015年11月11日 下午9:16:28
 * @version 1.00.00
 * @history:
 */
public class TestJsonTree {

 public static void main(String[] args) {
  
  EasyUiTree tree1=new EasyUiTree(); 
   tree1.setText("根選單");
   List<EasyUiTree> treenode=new ArrayList();
   for (int i=0;i<3;i++)
   {
    EasyUiTree tree=new EasyUiTree();
    tree.setText("一級"+i);   
    List<EasyUiTree> child=new ArrayList();
    for (int j=0;j<4;j++)
    {
     EasyUiTree stree=new EasyUiTree();
    stree.setText("二級選單"+i+""+j);
    Map<String,String> map=new HashMap();
    map.put("url", "url"+j);
    stree.setAttributes(map);
     child.add(stree);   
    }
    tree.setChildren(child);
    treenode.add(tree);
    }
  
   tree1.setChildren(treenode);
  
   String str=""; 
   System.out.println("test 開始轉化");
  str=  JSON.toJSONString(tree1);
 System.out.println(str);
System.exit(0);
 
 
 }

}

執行結果如下:

{"children":[{"children":[{"attributes":{"url":"url0"},"text":"二級選單00"},{"attributes":{"url":"url1"},"text":"二級選單01"},{"attributes":{"url":"url2"},"text":"二級選單02"},{"attributes":{"url":"url3"},"text":"二級選單03"}],"text":"一級0"},{"children":[{"attributes":{"url":"url0"},"text":"二級選單10"},{"attributes":{"url":"url1"},"text":"二級選單11"},{"attributes":{"url":"url2"},"text":"二級選單12"},{"attributes":{"url":"url3"},"text":"二級選單13"}],"text":"一級1"},{"children":[{"attributes":{"url":"url0"},"text":"二級選單20"},{"attributes":{"url":"url1"},"text":"二級選單21"},{"attributes":{"url":"url2"},"text":"二級選單22"},{"attributes":{"url":"url3"},"text":"二級選單23"}],"text":"一級2"}],"text":"根選單"}