简体   繁体   English

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

[英]How to format data into specific JSON format

Need help with some logic here--I'm trying to format my data in a specific way (parent-child relationship/tree format) and into json (I'm using gson). 在这里需要一些逻辑上的帮助-我正尝试以特定方式(父子关系/树格式)格式化数据并将其格式化为json(我正在使用gson)。 But I can't seem to get it right. 但我似乎无法正确解决。 Below has the closest format I was able to make and under it has the format I want it to output. 下面是我能制作的最接近的格式,下面是我要输出的格式。 This needs to return as a hashtable. 这需要作为哈希表返回。

One issue I was running into was putting [] brackets after "children". 我遇到的一个问题是将[]放在“孩子”后面。 The bracket is there after the first "children" but does not show up later. 括号在第一个“孩子”之后出现,但稍后不再显示。 My guess is because [] brackets only show up when I use HashSet. 我的猜测是因为[]方括号仅在使用HashSet时出现。 So perhaps, putting hashsets within hashsets could solve this particular issue? 因此,也许将哈希集放入哈希集中可以解决此特定问题?


The code I have for it is here: 我的代码在这里:

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;
   }

} }


What my code generates: 我的代码生成了什么:

{
   "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"
        }
     }
  }
   ]
}

What I want to output: 我要输出的是:

{
  "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"},
     ]
     },
     ]
  }
]

} }

I think you want something like this 我想你想要这样的东西

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));    
    }

}

that generates 产生

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

You see, "[" is the beginning of a JSON list/array 您会看到,“ [”是JSON列表/数组的开头

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM