javascript - Incorrect output post JSON.parse(...) -


the controller method :

@requestmapping(value = "channelintentiondetails.html", method = requestmethod.post)   public @responsebody   report getchannelintentiondetails(@requestbody searchparameters searchparameters) {     logger.info("in reportcontroller.getchannelintentiondetails(...), searchparameters " + searchparameters);      return channeldetailsservice.getintentiondetails(searchparameters);   } 

the report pojo :

public class report {    private map<string, collection<string>> parameterwiseresult;   private collection<string>              results;   private string                          result;   private string                          spid; . . . } 

the results collection holds json strings returned mongodb collection

the js ajax snippet :

    var xmlhttp = new xmlhttprequest();     xmlhttp.open("post","channelintentiondetails.html",false);     xmlhttp.setrequestheader("content-type","application/json");     xmlhttp.setrequestheader("accept","application/json");     xmlhttp.send(stringifiedsearchparameter);      alert("plain ajax response "+ xmlhttp.response);     alert("after json.parse(...) "+ (json.parse(xmlhttp.response)).results);  drawintentionpiechart((json.parse(xmlhttp.response)).results);  function drawintentionpiechart(data) {          //alert("in drawintentionpiechart(...) " + intentiongooglepiechart);          if (intentiongooglepiechart == null                 || intentiongooglepiechart == 'undefined') {             //alert("creating new intentionpiechart");             intentiongooglepiechart = new google.visualization.piechart(                     document.getelementbyid('intentionpiechart'));         }          intentiongooglepiechart.clearchart();          //var jsondata = json.parse(data);         var jsondata = data;          var data = new google.visualization.datatable();         data.addcolumn('string', 'intention');         data.addcolumn('number', 'share');          (i = 0; < jsondata.length; i++) {              /* alert("intention : " + jsondata[i]._id.intention_category                     + " count : " + jsondata[i].count); */              data.addrows([ [ jsondata[i]._id.intention_category,                     parseint(jsondata[i].count) ] ]);          }          var options = {             title : 'intention analysis',             titletextstyle : {                 color : '#0e5eae'             },             fontsize : 14,             width : 390,             height : 200          };          intentiongooglepiechart.draw(data, options);     } 

first alert "results" array

plain ajax response {"parameterwiseresult":null,"results":["{ \"_id\" : { \"spid\" : 352 , \"intention_category\" : \"opine\" , \"report_id\" : 2 , \"channel_id\" : 1} , \"count\" : 1}","{ \"_id\" : { \"spid\" : 352 , \"intention_category\" : \"wish,purchase\" , \"report_id\" : 2 , \"channel_id\" : 1} , \"count\" : 1}","{ \"_id\" : { \"spid\" : 352 , \"intention_category\" : \"complain\" , \"report_id\" : 2 , \"channel_id\" : 1} , \"count\" : 1}","{ \"_id\" : { \"spid\" : 352 , \"intention_category\" : \"purchase\" , \"report_id\" : 2 , \"channel_id\" : 1} , \"count\" : 2}","{ \"_id\" : { \"spid\" : 352 , \"intention_category\" : \"none\" , \"report_id\" : 2 , \"channel_id\" : 1} , \"count\" : 93}"],"result":null,"spid":null,"idvallistsearchprofile":null,"idvallisttags":null,"spmaster":null,"competitiveparameters":null} 

second alert after json.parse(...), array braces viz. [] gone :

after json.parse(...) { "_id" : { "spid" : 352 , "intention_category" : "opine" , "report_id" : 2 , "channel_id" : 1} , "count" : 1},{ "_id" : { "spid" : 352 , "intention_category" : "wish,purchase" , "report_id" : 2 , "channel_id" : 1} , "count" : 1},{ "_id" : { "spid" : 352 , "intention_category" : "complain" , "report_id" : 2 , "channel_id" : 1} , "count" : 1},{ "_id" : { "spid" : 352 , "intention_category" : "purchase" , "report_id" : 2 , "channel_id" : 1} , "count" : 2},{ "_id" : { "spid" : 352 , "intention_category" : "none" , "report_id" : 2 , "channel_id" : 1} , "count" : 93} 

later,i errors when try iterate parsed result.

typeerror: jsondata[i]._id undefined

data.addrows([ [ jsondata[i]._id.intention_category, parseint(jsondata[i].count) ] ]);  

where messing up?

there 2 solutions fix problem.

option 1: change data format returned service

your pogo should below, if want return collection/array of objects web service.

public class report {      private map<string, collection<string>> parameterwiseresult;     private collection<myobject>            results;     private string                          result;     private string                          spid;      //setters , getters }  public class myobject {      private id _id;     private int count;      //setters , getters      private class id {          int spid;          string intention_category;          int report_id;          int channel_id;           //setters , getters      } } 

option 2: double parse string convert inner string object in javascript. within loop, need parse each element of array json below:

var myjson = json.parse(jsondata[i]); data.addrows([ [ myjson._id.intention_category, parseint(myjson.count) ] ]);  

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -