【问题标题】:unable to get the exact string from json response using jquery无法使用 jquery 从 json 响应中获取确切的字符串
【发布时间】:2012-12-03 20:55:01
【问题描述】:

我有一个表单,如果我提交它,那么我希望在同一页面中从我的 json 响应中打印成功消息。

我从 json 响应中获得了许多值,但我只想要在我的 java 操作中定义的名为“消息”的变量。

格式化的json响应是这样的

       `{"errmsg":null,"jsonModel":null,"message":"Succussfuyly completed the task","model":
        {"createdDate":null,"createrId":null,"id":null,"themeCaption":null,"themeName":null,
        "themeScreenshot":null,"updateId":null,"updatedDate":null},"oper":null,"theme":{"createdDate":null,
        "createrId":null,"id":null,"themeCaption":null,"themeName":null,"themeScreenshot":null,
        "updateId":null,"updatedDate":null},"themeScreenshot":null}`

从这个响应中,我只想在我的 jsp 页面中打印消息变量。 从我下面的 jquery 代码中,所有值都打印在我的 jsp 页面中。但我只想要消息变量。

index.jsp

  <script type="text/javascript">
    $(document).ready( function() {
        $.subscribe('handleJsonResult', function(event,data) {
            $('#result').html("<div id='languagesList'> <s:property value="Message"/> </div>"+'' + data.Message + '');
            var list = $('#languagesList');
                    $.each(event.originalEvent.data, function(index, value) { 
                            list.append('<h1>'+value+'</h1>\n');
                    });
        });
    });    
        </script>  
        </head>
        <body>

     <img id="indicator" src="${pageContext.request.contextPath}/images/others/ajax-loader.gif" alt="Loading..." style="display:none"/>
    <s:form action="updatethemeimageform" method="post" enctype="multipart/form-data" id="remoteform"  theme="simple" > 
               <s:hidden value="%{#parameters.themeid}" name="themId"/> 
               <s:file name="themeScreenshot" label="Theme Screenshot" /> 
              <sj:a button="true" id="btnsid" buttonIcon="ui-icon-gear" dataType="json" indicator="indicator"  onSuccessTopics="handleJsonResult" 
              formIds="remoteform" targets="result"  >Submit This Form</sj:a> 
     </s:form>

         <sj:div id="result" >    
            Json Result will come here
         </sj:div>



                       `-----------
                         ---------------
                    private String Message;

           public String updateThemesImage(){ 
    setMessage("Succussfuyly completed the task"); 
    return SUCCESS;
              }
                   ------------
                    -----------
                   With getter & setters`

从我上面的 jquery 函数我得到这样的输出 请帮我解决这个问题

【问题讨论】:

  • 您需要在 JSP 技术中使用的任何函数中解析 JSON 响应并输出该变量。
  • @adityamenon 怎么做?这是我的第一个 json 程序。你能解决我的问题吗
  • 如果 JSON 来自页面内的资源,为什么它首先作为事件数据中的字符串而不是对象发送?我假设您正在将一个对象转换为 JSON,然后您需要再次将其转换回来,而不是使用原始对象。
  • 如果 JSON 来自 jQuery AJAX...只需将 dataType:'json' 添加到 AJAX 选项,响应将被转换为 jQuery 核心内的对象
  • @charlietfl 您能否将其发布为我上述问题的答案

标签: json jquery jquery-plugins struts2-jquery


【解决方案1】:

您的 each 循环工作正常,但是如果您只想要几个特定的​​变量,请使用对象表示法来检索它们:

$.subscribe('handleJsonResult', function(event, data) {
    $('#result').html("<div id='languagesList'> <s:property value="Message "/> </div>" + '' + data.Message + '');
    var list = $('#languagesList');

    var jsonData = event.originalEvent.data;
        /* example retrieving ID and message*/
    list.append('<h1>ID:' + jsonData.id + ', Message:' + jsonData.message +'</h1>\n');

});

【讨论】:

  • 是的,它会自动将我的 json 响应转换为数据 ($.subscribe('handleJsonResult', function(event,data) {)。但是从数据中我怎样才能得到特定的变量
  • 好的...我以为它已经是一个对象了...所有parseJSON 的想法都偏离了轨道。将更新答案
  • updated... 非常不清楚您到底想从数据中得到什么。还有其他几种方法,具体取决于您想要什么
  • 从数据中我只想要消息变量
  • 好的,我给你的应该这样做......只需删除 ID 部分
猜你喜欢
  • 2016-08-28
  • 1970-01-01
  • 2014-07-19
  • 2017-12-20
  • 1970-01-01
  • 2019-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多