【问题标题】:How to retrieve data if stored as object?如果存储为对象,如何检索数据?
【发布时间】:2019-04-26 04:27:37
【问题描述】:

我正在使用 ajax 将数据传递给 servlet。据我所知,标准方式通常是这样的:

$.ajax({

               url: 'page.jsp',
               type: 'POST',
               data: {

                     id:value,
                     name:value2

               },

               success: function (data) {
                   alert("Successfully initiated email to queue");
               },
               error: function (request, error) {
                   alert("Request: " + JSON.stringify(error));
               }
           });

在 jsp 页面中,可以这样检索数据:

String id = request.getParameter("id");
String name = request.getParameter("name");

这无疑会奏效。现在,如果我想将数据存储为对象怎么办。在我的 JavaScript 中是这样的:

 var data;

     if(condition){

       data={
                'recipient': recipient,
                'subject': subject,
                'content': content,
                'id':"<%=id%>",
                'hash':"<%=hash%>",
                'multiemail':"no"

            }

    }else{

          data= {
                'recipient': recipient,
                'subject': subject,
                'content': content,
                'arrayList':<%=array%>,
                'multiemail':"yes"

            }

    }
  $.ajax({

               url: 'page.jsp',
               type: 'POST',
               data: {

                     info:data

               },

               success: function (data) {
                   alert("Successfully initiated email to queue");
               },
               error: function (request, error) {
                   alert("Request: " + JSON.stringify(error));
               }
           });

然后,我用同样的方式:

 String recipient = request.getParameter("recipient");

这将返回空值。如何从对象中准确检索我想要的数据值?

【问题讨论】:

  • stackoverflow.com/questions/34179986/… 看看你能不能在这里找到类似的东西
  • @AkshayMulgavkar:他提到了servlet。他正在尝试在 Java 中获取 JSON 值
  • @AkshayMulgavkar 不,这不是我想要的。
  • 没有时间写完整的回复,但你应该在谷歌上搜索java jackson 了解如何处理 JSON 数据(js 对象的序列化形式)并阅读这个答案以了解如何获取该数据:stackoverflow.com/questions/14525982/…
  • 根据您提供的链接,它告诉我使用request.getreader。但是我究竟如何获得对象中的“特定”数据?

标签: javascript java ajax


【解决方案1】:

当你使用下面这行代码String recipient = request.getParameter("recipient"); 它在

中的数据中查找收件人密钥
           $.ajax({
           url: 'page.jsp',
           type: 'POST',
           data: {

                 info:data

           }

但是很幸运没有收件人,您的 ajax 中只有 info 键。 因此,您可以使用 getParameter("info") 来获取 数据。现在你有你的数据了。

参考以下代码

$.ajax({
           url: 'page.jsp',
           type: 'POST',
           data: data

我想现在你可以使用String recipient = request.getParameter("recipient");

【讨论】:

  • 但是我想获取info中的特定数据,例如recipient,这可能吗?
  • 是的,有可能,您可以使用 getAttribute() 方法而不是 getParameter()
  • 那么代码会是什么样子?请提供充分的解释
  • 现在我用一个新的代码段编辑了答案,如果有任何事情请查看并更新我
  • 它不起作用,因为我知道它不是这样工作的
【解决方案2】:

request.getParameter("recipient"); 将在您的数据中查找收件人密钥。但是您的关键是info 而不是recipient(这是信息的一部分)。要访问收件人,您必须首先使用任何 JSON 解析库解析收到的 JSON (request.getParameter("info")),然后从解析的 JSON 对象访问收件人。

在您的 ajax 中,以 json 格式传递您的数据

$.ajax({

           url: 'page.jsp',
           type: 'POST',
           dataType: 'JSON',
           data: {
                 info:data
           },

           success: function (data) {
               alert("Successfully initiated email to queue");
           },
           error: function (request, error) {
               alert("Request: " + JSON.stringify(error));
           }
       });

在您的 servlet 端,像这样解析 json:

JsonParser parser = new JsonParser();

String json = request.getParameter("info");

JsonElement jsonTree = parser.parse(json);
String recipientjsonTree.get("recipient");

JsonParser 是 GSON 库的一部分。

【讨论】:

  • 是的。如果您的 info 是正确的 JSON 格式,它肯定会起作用
  • 等我测试一下
  • ` JsonParser parser=new JsonParser();` 出错,我需要在括号中输入内容
  • 我使用 jsp 作为我的 servlet。希望没事
  • 如果您遇到任何与解析相关的异常,那么您的 info 对象的 json 格式不正确
猜你喜欢
  • 2020-08-09
  • 1970-01-01
  • 2017-04-03
  • 1970-01-01
  • 2022-11-02
  • 2014-02-09
  • 1970-01-01
  • 1970-01-01
  • 2021-08-11
相关资源
最近更新 更多