如何在 JSP 中编写多维 JSON 对象并将 JSON 对象传递回 JavaScript?

发布于 2024-11-06 08:46:45 字数 916 浏览 0 评论 0原文

我目前正在尝试在 JSP 中编写多维对象或数组,然后将其传递回 JavaScript 的 AJAX 调用。现在,我已经弄清楚使用 AJAX 解码 JSON 对象(jQuery JSON)。所以,我在这方面很擅长。

但我对在 JSP 中创建多维 JSON 对象或数组感到困惑。

我一直在寻找 JSP 的 JSON 插件的 json-simple (http://code.google.com/p/json-simple/)。而且不仅仅是这个插件,我一直无法在JSP中找到任何多维JSON对象或数组示例的示例。

就像,我得到这个例子,但它是一维的:

//import org.json.simple.JSONObject;

JSONObject obj=new JSONObject();
obj.put("name","foo");
obj.put("num",new Integer(100));
obj.put("balance",new Double(1000.21));
obj.put("is_vip",new Boolean(true));
obj.put("nickname",null);
System.out.print(obj);

我希望 JSON 对象有这样的结果:

{
  "results": [ {
    "address_components": [ {
      "long_name": "1600",
      "short_name": "1600",
      "types": [ "street_number" ]
    } ],
  } ]
}

然后,我将它传递回 JavaScript 并对其进行解码。

总结一下:如何在 JSP 中创建多维对象或数组? JSON 插件无关紧要;无论什么有效,我都会非常高兴。

我将不胜感激任何帮助。先感谢您。

I am currently trying to write a multidimensional object or array in JSP and then pass it back to an AJAX call from JavaScript. Now, decoding a JSON object using AJAX I have figured out (jQuery JSON). So, I'm good on that front.

But I am lost on creating a multidimensional JSON object or array in JSP.

I have been looking at json-simple for the JSON plugin for JSP (http://code.google.com/p/json-simple/). And it's not just this plugin, but I have been unable to find any examples of multidimensional JSON objects or array examples in JSP.

Like, I get this example but it's one-dimensional:

//import org.json.simple.JSONObject;

JSONObject obj=new JSONObject();
obj.put("name","foo");
obj.put("num",new Integer(100));
obj.put("balance",new Double(1000.21));
obj.put("is_vip",new Boolean(true));
obj.put("nickname",null);
System.out.print(obj);

I would like the JSON object to have a result like this:

{
  "results": [ {
    "address_components": [ {
      "long_name": "1600",
      "short_name": "1600",
      "types": [ "street_number" ]
    } ],
  } ]
}

Then, I'd pass it back to JavaScript and decode it.

To Sum up: How can I create a multidimensional object or array in JSP? The JSON plugin is inconsequential; whatever works, I'll be more than happy.

I would appreciate any help. Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灯下孤影 2024-11-13 08:46:45

要使用 Gson 创建这样的结构:

{
  "results": [ {
    "address_components": [ {
      "long_name": "1600",
      "short_name": "1600",
      "types": [ "street_number" ]
    } ],
  } ]
}

您可以执行以下操作:

<%
String[] types = {"street_number"};
Hashtable address_components = new Hashtable();
address_components.put("long_name", 1600);
address_components.put("short_name", 1600);
address_components.put("types", types);
Hashtable results = new Hashtable();
results.put("address_components", address_components);

// Make sure you have the Gson JAR in your classpath
// (place it in the tomcat/classes directory)
com.google.gson.Gson gson = new com.google.gson.Gson();
String json = gson.toJson(obj);  
out.print(json);
%>

To create a structure like this using Gson:

{
  "results": [ {
    "address_components": [ {
      "long_name": "1600",
      "short_name": "1600",
      "types": [ "street_number" ]
    } ],
  } ]
}

You could do something like this:

<%
String[] types = {"street_number"};
Hashtable address_components = new Hashtable();
address_components.put("long_name", 1600);
address_components.put("short_name", 1600);
address_components.put("types", types);
Hashtable results = new Hashtable();
results.put("address_components", address_components);

// Make sure you have the Gson JAR in your classpath
// (place it in the tomcat/classes directory)
com.google.gson.Gson gson = new com.google.gson.Gson();
String json = gson.toJson(obj);  
out.print(json);
%>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文