java - How to access elements of JSON array in javascript? -


i'm creating arraylist in java, running .tojson function google-gson on it:

public string statusestojson(arraylist<string> statuses){     gson gson = new gson();     return gson.tojson(statuses); } 

which results in json:

[ "u", "u", "u", "u" ] 

then in jsp i'm passing javascript:

<script language="javascript" type="text/javascript">checkstatus.loaded('<%=model.getpageid() %>', '<%=request.getcontextpath() %>', '<%=model.arraylisttojson(model.getstatuses()) %>');</script> 

then in javascript i'm parsing json array:

checkstatus.statuses = json.parse(statuses); alert(checkstatus.statuses); 

this results in following output:

u, u, u, u 

the problem following doesn't work , causes page not load:

alert(checkstatus.statuses[0]); 

what's wrong this?

edit: loaded function:

loaded : function(guid, context, statuses) {         checkstatus.guid = guid;         checkstatus.context = context;         checkstatus.statuses = json.parse(statuses);         alert(checkstatus.statuses[0]);          if(checkstatus.init == null){             submitform('checkstatusform', checkstatus.guid);             checkstatus.init = true;         }          setupform('checkstatusform', function(){checkstatus.validstatus();});          //checkstatus.setimages();          applycss3('check_status');     } 

valid status function:

validstatus : function(){         checkstatus.params = $('#checkstatusform').serializeobject();          if(document.getelementbyid('regionid').value != "" && document.getelementbyid('regionaction').value != ""){             submitform('checkstatusform', checkstatus.guid);         }else{             error("cannot commit", "you must select action before attempting commit.");         }     }, 

setup form function:

/**  * sets form submit when user presses enter inside input  * element. calls callback when form submitted, not  * submit form.  *   * @param id id of form.  * @param callback callback call.  * @return nothing.  */ function setupform(id, callback) {     $('#' + id + ' input').keydown(function(e) {         if (e.keycode === 13) {             $(this).parents('form').submit();             e.preventdefault();         }     });     $('#' + id).submit(function(e) {         e.preventdefault();         callback();     }); } 

submit form function:

/**  * serializes , submits form.  *   * @param id  *            id of form submit.  * @param guid  *            guid of page form on pass server.  * @return nothing.  */ function submitform(id, guid) {     var subtabid = $('#' + id).closest('#tabs > div > div').attr(             'id'), tabid = $('#' + id).closest('#tabs > div')             .attr('id'), data = $('#' + id).serializearray();     data.push( {         name : "framework-guid",         value : guid     });     $.ajax( {         type : 'post',         cache : 'false',         url : '/pasdash-web/' + tr("_", "", tabid.tolowercase()) + '/' + tr("_", "", subtabid)                 + '.jsp',         data : data,         success : function(html) {             $('#' + subtabid).html(html);             resourcechanged(tabid, subtabid,                     $('#' + id + ' input[name="framework_command"]')[0].value,                     guid);         },         error : function(jqxhr, textstatus, errorthrown) {             error('ajax error', textstatus);         }     });     return false; } 

you don't need wrap json strings, force have reparse it. try removing quotes , not calling json.parse

loaded : function(guid, context, statuses) {     checkstatus.guid = guid;     checkstatus.context = context;     // here's change     checkstatus.statuses = statuses;     alert(checkstatus.statuses[0]); 

and change html be

<script type="text/javascript">     checkstatus.loaded('<%=model.getpageid() %>',                         '<%=request.getcontextpath() %>',                        // following line should output                        // ["a", "b"]                        // valid javascript                        <%=model.arraylisttojson(model.getstatuses()) %>); </script> 

Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -