无法使用jquery从json响应中获取确切的字符串

时间:2012-12-16 15:18:52

标签: json jquery jquery-plugins struts2-jquery

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

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

格式化的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函数我得到的输出就像这样Output of my above jquery function 请帮我解决这个问题

1 个答案:

答案 0 :(得分: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');

});