Search code examples
extjsextjs4

Built a JSON from string pieces


I work with a line chart in ExtJS4 now. The chart is based on the data of the store. The store change its data with help 'loadRawData()' function. Familiar situation, isn't it?

AJAX sends strings every 10 seconds and I need to built JSON from this pieces of strings. I'm trying:

success: function(response) {
    var requestMassive = JSON.parse(response.responseText);
    var jArray = [];
                for(var i=0;i<requestMassive.length;i++){
                    var firstPiece = JSON.parse(response.responseText)[i].date; 
                    var secondPiece = JSON.parse(response.responseText)[i].connectCount;
                    var recording = "{'machinesPlayed':"+firstPiece+", 'machinesOnline':"+secondPiece+"}";
                    jArray.push(recording);
                }

                jArray = '['+jArray+']';
                store.loadRawData(jArray);
}

But it is wrong way. How to do it properly?


Solution

  • You could use loadData() function instead of loadRawData(). loadData() only needs an array of objects.

    success: function(response) {
       var requestMassive = JSON.parse(response.responseText);
       var jArray = [];
          for(var i=0;i<requestMassive.length;i++){
             jArray.push({ machinesPlayed: requestMassive[i].date, machinesOnline: requestMassive[i].connectCount});
          }
    
          store.loadData(jArray);
    }