javascript - JSON Array Value Returns "[{" -
i writing jsp web app , have converted list of java objects json array, , set attribute. right trying use jquery parse array , make separate json objects, have hit big snag , can't find online else has dealt it. servlet creates formatted json array when access value of array so:
orders[0].value;
i returned "[{" in javascript console. when access object
orders[0];
which hidden input holds array, find value looks this:
value="[{" firstname" : "john", "lastname" : "doe", .....
as can see, looks there newline after "[{" , recognizing value. in javascript console, rest of array highlighted differently well.
i totally stumped on how solve issue. advice appreciated.
here's code servlet sets attribute:
object orders = request.getsession().getattribute("orders"); string json = new gson().tojson(orders); try { arraylist<string> jsonlist = extractobjects(json); httpsession session = request.getsession(); session.setattribute("jsonorders", jsonlist); //all prints on 1 line system.out.println(jsonlist); } catch (exception e) { system.out.println("problem parsing json"); } } enum parserstate { reading_object, reading_array }; //create json array private arraylist<string> extractobjects(string array) throws parseexception { parserstate state = parserstate.reading_array; stringbuilder currentobject = null; arraylist<string> result = new arraylist<string>(); int = 0; int parenthesisbalance = 0; (char c : array.tochararray()) { switch (c) { case '{': { if (state == parserstate.reading_array) { state = parserstate.reading_object; currentobject = new stringbuilder(); } parenthesisbalance++; currentobject.append(c); break; } case '}': { if (state == parserstate.reading_array) { throw new parseexception("unexpected '}' ", i); } else { currentobject.append(c); parenthesisbalance--; if (parenthesisbalance == 0) { state = parserstate.reading_array; result.add(currentobject.tostring()); } } break; } default: { if (state == parserstate.reading_object) { currentobject.append(c); } } } i++; } return result; }
here's input store attribute:
<input type="hidden" value="${ sessionscope.jsonorders }" id="orderdata" />
when accessed javascript using $('#orderdata'), ,
<input type="hidden" value="[{" firstname":"mike","lastname":"slagle","phonenumber":"17248802249","email":"[pslagle12@gmail.com]","duedate":"2016-11-24","product":"cake","comments":"this="" is="" 11242016="" 11.15","id":16,"price":11.15},="" {"firstname":"mike","lastname":"slagle","phonenumber":"17248802249","email":"[pslagle12@gmail.com]","duedate":"2016-11-24","product":"cake","comments":"this="" 11.15","id":17,"price":11.15},="" {"firstname":"patrick","lastname":"slagle","phonenumber":"7248802249","email":"[pslagle12@gmail.com]","duedate":"2016-11-24","product":"cookies","comments":"this="" cookies="" for="" patrick.","id":18,"price":15.66},="" {"firstname":"betsy","lastname":"horton","phonenumber":"7245443344","email":"[bhorton@hotmail.com]","duedate":"2016-12-17","product":"cupcakes="" ","comments":"this="" cupcakes="" bets.","id":19,"price":65.98},="" {"firstname":"morgan","lastname":"freeman","phonenumber":"864667234","email":"[freeman@hotmail.com]","duedate":"2016-02-15","product":"cake","comments":"this="" a="" cake="" morgan.","id":20,"price":200.75},="" {"firstname":"james","lastname":"boice","phonenumber":"7249983532","email":"[jboice@deadoldguy.net]","duedate":"2016-04-16","product":"cake="" and="" cookies","comments":"this="" an="" 18th="" century="" theologian","id":21,"price":54.95},="" theologian","id":22,"price":54.95}]"="" id="orderdata">
and way accessing value returns "[{":
var orders = $('#orderdata') console.log(orders[0].value);
edit
if jsp:
<c:foreach var="i" items="${ jsonorders }"> ${ } </c:foreach>
the contents of entire json array printed on page, want. seems i'm dealing weird interplay between jquery , json arrays. suggestions appreciated. i'm stumped. thing gives me value array orders[0].value, gives me "[{".
where orders comes in this?? according view, have seen json value assigned variable value.
json value assigned string i.e value="[{...}]"
, pls remove qoutes , assign value=[{....}]
then access field values value[0]["firstname"] or value[0].firstname
value john.
Comments
Post a Comment