如何将数据格式化为特定的JSON格式

时间:2014-01-29 22:03:33

标签: json hashtable gson

在这里需要一些逻辑帮助 - 我试图以特定方式(父子关系/树格式)和json(我正在使用gson)格式化我的数据。但我似乎无法做对。下面有我能够制作的最接近的格式,在它下面有我希望它输出的格式。这需要作为哈希表返回。

我遇到的一个问题是在“孩子”之后加上[]括号。在第一个“孩子”之后,括号位于此处,但之后不会显示。我的猜测是因为[]括号只在我使用HashSet时出现。那么也许,将hashsets放在hashsets中可以解决这个特殊问题吗?


我的代码就在这里:

public class DendrogramPlaySheet extends BrowserPlaySheet {
public Hashtable processQueryData() {
          HashSet food = new HashSet();

          String[] var = wrapper.getVariables();

          for (int i=0; i<list.size(); i++){
                 LinkedHashMap foodType = new LinkedHashMap();
                 LinkedHashMap foodItem = new LinkedHashMap();
                 LinkedHashMap foodFlavor = new LinkedHashMap();

                 //this is a table of data that we get from a query
                 Object[] listElement = list.get(i);

                 //taking columns and putting them into the hashmap
                 foodType.put("name", listElement[0]);
                 foodItem.put("name", listElement[1]);
                 foodFlavor.put("name", listElement[2]);

                 foodItem.put("children", foodFlavor);
                 foodType.put("children", foodItem);

                 food.add(foodType);
          }

          Hashtable allHash = new Hashtable();

          allHash.put("name", "Food");
          allHash.put("children", food);
          return allHash;
   }

}


我的代码生成的内容:

{
   "name":"Food",
   "children":[
  {
     "name":"Italian",
     "children":{
        "name":"Pizza",
        "children":{
           "name":"Cheese"
        }
     }
  },
  {
     "name":"American",
     "children":{
        "name":"Hamburgers",
        "children":{
           "name":"Plain"
        }
     }
  },
  {
     "name":"Italian",
     "children":{
        "name":"Pasta",
        "children":{
           "name":"Pesto"
        }
     }
  },
  {
     "name":"Italian",
     "children":{
        "name":"Cannoli",
        "children":{
           "name":"Plain"
        }
     }
  },
  {
     "name":"Italian",
     "children":{
        "name":"Pizza",
        "children":{
           "name":"Pepperoni"
        }
     }
  },
  {
     "name":"Mexican",
     "children":{
        "name":"Burritos",
        "children":{
           "name":"Beef"
        }
     }
  }
   ]
}

我想输出的内容:

{
  "name":"Food",
  "children":[
  {
     "name":"Italian",
     "children":[
     {
        "name":"Pizza",
        "children":[
           {"name":"Pepperoni"},
           {"name":"Cheese"},
     ]
     },
     {
        "name":"Cannoli",
        "children":[
           {"name":"Plain"},
     ]
     },
     {
        "name":"Pasta",
        "children":[
           {"name":"Pesto"},
     ]
     },
     ]
  },
  {
     "name":"American",
     "children":[
     {
        "name":"Hamburgers",
        "children":[
           {"name":"Plain"},
           ]
     },
     ]
  },
  {
     "name":"Mexican",
     "children":[
     {
        "name":"Burritos",
        "children":[
           {"name":"Beef"},
     ]
     },
     ]
  }
]

}

1 个答案:

答案 0 :(得分:0)

我想你想要这样的东西

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

import com.google.gson.Gson;

public class DendrogramPlaySheet {
    public static void main(String[] args){
        Map m = new HashMap();
        m.put("name", "food");

        Map m3 = new HashMap();
        m3.put("name", "peperoni");

        Map m4 = new HashMap();
        m4.put("name", "cheese");

        List list2 = new ArrayList();
        list2.add(m3);
        list2.add(m4);

        Map m2 = new HashMap();
        m2.put("name", "pizza");
        m2.put("children", list2);

        List list = new ArrayList();
        list.add(m2);

        m.put("children", list);

        Gson gson = new Gson();
        System.out.println(gson.toJson(m));    
    }

}

生成

{
   "name":"food",
   "children":[
      {
         "name":"pizza",
         "children":[
            {
               "name":"peperoni"
            },
            {
               "name":"cheese"
            }
         ]
      }
   ]
}

你看,“[”是JSON列表/数组的开头