如何在 JSP/Java 中创建多维 HashMap 或 Hashtable 并将其转换为 JSON 对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5981647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I create a multidimensional HashMap or Hashtable in JSP / Java and convert it to a JSON object?
提问by user717236
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.
我需要帮助在 JSP 中创建多维 HashMap 或 Hashtable。为什么我需要 HashMap 或 HashTable?因为我最终想将一个 JSON 对象传回给客户端。如果有另一种方法可以最终到达 JSON 对象,我完全听得懂。
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?
如何在 JSP 中编写多维 JSON 对象并将 JSON 对象传递回 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:
这是我的 JSP 代码,它使用了一个简单的例子,而不是像上面那样的真实世界数据:
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:
但是 JSON 对象看起来像这样:
{
"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.
这不是想要的结果。它只获取最后一个结果并将其转换为 JSON。因此,问题始于未正确构建多维哈希。不过,我不确定是什么问题。我会很感激一些帮助。谢谢你。
回答by BalusC
In JSON, the {}
is mappable as Java Map
and the []
is mappable as Java List
.
在 JSON 中,{}
可映射为 Java Map
,[]
可映射为 Java List
。
So, to achieve the following JSON format,
因此,要实现以下 JSON 格式,
{
"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>>>>>
.
你需要一个(深呼吸)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>
.
更好的是只使用完整的 Javabeans 而不是Map<String, String>
.
回答by rlawson
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);
回答by lutzh
The line
线
results_hash.put("results",arr[i]);
results_hash.put("结果",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.
还没有测试过,但我希望你能明白……你需要正确地获得初始数据结构。