如何在JSP/Java中创建多维HashMap或Hashtable并将其转换为JSON对象? [英] How do I create a multidimensional HashMap or Hashtable in JSP / Java and convert it to a JSON object?

查看:139
本文介绍了如何在JSP/Java中创建多维HashMap或Hashtable并将其转换为JSON对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在JSP中创建多维HashMap或Hashtable的帮助.为什么我需要HashMap或HashTable?因为我最终想将一个JSON对象传递回客户端.如果还有另一种最终到达JSON对象的方法,那么我很烦.

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:

这就是我想要的结果看起来像什么:

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

这是我的JSP代码,它使用一个简单的示例,而不是上面的真实数据:

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

但是JSON对象看起来像这样:

But the JSON object looks like this:

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

这不是期望的结果.它仅获取最后的结果并将其转换为JSON.因此,问题开始于多维散列的构建不正确.不过,我不确定是什么问题.我将不胜感激.谢谢.

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中,{}可映射为Java Map,而[]可映射为Java List.

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

因此,要实现以下JSON格式,

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

您需要(深呼吸)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!

更好的方法是只使用完全有价值的Javabean,而不是Map<String, String>.

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

这篇关于如何在JSP/Java中创建多维HashMap或Hashtable并将其转换为JSON对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆