How do I create a multidimensional HashMap or Hashtable in JSP / Java and convert it to a JSON object?

谁说胖子不能爱 提交于 2019-12-12 16:33:00

问题


I need help creating a multidimensional HashMap or Hashtable in JSP. Why I do I need HashMap or HashTable? Because I ultimately want to pass back to the client a JSON object. If there is another way to ultimately arrive at a JSON object, I'm all ears.

I also wanted to mention that this thread has been invaluable and I've been expanding on it:

How can I write a multidimensional JSON object in JSP and pass the JSON object back to JavaScript?

Here is what I want the result to looks like:

{
  "results": [ {
    "address_components": [ {
      "long_name": "1600",
      "short_name": "1600"
    }, {
      "long_name": "Amphitheatre Pkwy",
      "short_name": "Amphitheatre Pkwy"
    }, {
      "long_name": "Mountain View",
      "short_name": "Mountain View"
    }, {
      "long_name": "California",
      "short_name": "CA"
    }, {
      "long_name": "United States",
      "short_name": "US"
    }, {
      "long_name": "94043",
      "short_name": "94043"
    } ]
  } ]
}

Here is my JSP code, which uses a trivial example, instead of real-world data like above:

Hashtable results_hash = new Hashtable();   
Hashtable numbers = new Hashtable();
Hashtable[] arr = new Hashtable[10];

for (int i=0; i < 10; i++)
{
  numbers.put("Number",i);
  numbers.put("Numberx2",i*2);
  arr[i] = new Hashtable();
  arr[i].put("Comp",numbers);
  results_hash.put("results",arr[i]);
}

com.google.gson.Gson gson = new com.google.gson.Gson();
String json = gson.toJson(results_hash);
out.print(json);

But the JSON object looks like this:

{
  "results": {
    "Comp":    {
      "Numberx2":18,
      "Number":9 
    }
  }
}

This is not the desired result. It's only taking the last result and converting it to JSON. So, the problem starts with the multidimensional hash not being built correctly. I'm not sure what is the problem, though. I would appreciate some help. Thank you.


回答1:


In JSON, the {} is mappable as Java Map and the [] is mappable as Java List.

So, to achieve the following JSON format,

{
  "results": [ {
    "address_components": [ {
      "long_name": "1600",
      "short_name": "1600"
    }, {
      "long_name": "Amphitheatre Pkwy",
      "short_name": "Amphitheatre Pkwy"
    }, {
      "long_name": "Mountain View",
      "short_name": "Mountain View"
    }, {
      "long_name": "California",
      "short_name": "CA"
    }, {
      "long_name": "United States",
      "short_name": "US"
    }, {
      "long_name": "94043",
      "short_name": "94043"
    } ]
  } ]
}

you need a (deep breathe) Map<String, List<Map<String, List<Map<String, String>>>>>.

List<Map<String, String>> addressComponents = new ArrayList<Map<String, String>>();
Map<String, String> addressComponent1 = new HashMap<String, String>();
addressComponent1.put("long_name", "1600");
addressComponent1.put("short_name", "1600");
addressComponents.add(addressComponent1);
Map<String, String> addressComponent2 = new HashMap<String, String>();
addressComponent2.put("long_name", "Amphitheatre Pkwy");
addressComponent2.put("short_name", "Amphitheatre Pkwy");
addressComponents.add(addressComponent2);
// ...

List<Map<String, List<Map<String, String>>>> results = new ArrayList<Map<String, List<Map<String, String>>>>();
Map<String, List<Map<String, String>>> result1 = new HashMap<String, List<Map<String,String>>>();
result1.put("address_components", addressComponents);
results.add(result1);
// ...

Map<String, List<Map<String, List<Map<String, String>>>>> god = new HashMap<String, List<Map<String,List<Map<String,String>>>>>();
god.put("results", results);
String json = new Gson().toJson(god);
System.out.println(json); // There!

Better is to just use fullworthy Javabeans instead of Map<String, String>.




回答2:


try this

for (int i=0; i < 10; i++)
{
   numbers.put("Number",i);
   numbers.put("Numberx2",i*2);
   arr[i] = new Hashtable();
   arr[i].put("Comp",numbers);
}
results_hash.put("results",arr);



回答3:


The line

results_hash.put("results",arr[i]);

will overwrite the last entry with the same key in your Hashtable. Your just replacing the entry with the key "results", not adding to it.

Try something like (pseudocode):

Map<String,String> entry;
Map<String, Map> results = new HashMap<String, Map>();
Map<String,List<Map> address_components = new HashMap<String, List<Map>>();
List<Map> entries = new ArrayList<Map>();

for 1..10 {
   entry = new HashMap<String,String>();
   entry.put("long_name", xxx);
   entry.put("short_name", xxx);
}

address_components.put("address_components", entries);
result.put("result", address_components);

Haven't tested it, but I hope you get the idea... you need to get the inital data structure right.



来源:https://stackoverflow.com/questions/5981647/how-do-i-create-a-multidimensional-hashmap-or-hashtable-in-jsp-java-and-conver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!