How to extract data from JSON object and how the JSON object is defined

Share RGraph:  

« Back to message list

Enter your email address to get email updates on this topic. You can stop receiving updates by clicking the link in the update email messages.

Posted by hardeep on 23rd September 2015
how to extract data from json object and how the json object is defined on the other page.

like this example www.rgraph.net/docs/ajax-functions.html

i want to extract the values from the json object and my json object is

{
"name":["Temperature","",""],
"value":["50,0,0,0,0","0,0,0,0,0","0,0,0,0,0","0,0,0,0,0","0,0,0,0,0"],
"color":["RED"]}

and how to extract data from this json object
Posted by hardeep on 23rd September 2015
i got error:-

RGraph.line.js:50 Uncaught TypeError: Cannot read property 'getContext' of null

in my console
Posted by Richard Heyes on 23rd September 2015
Hi there,

You need to convert the data in your object to real numeric values instead of strings as it is now. You can do that like this:


var obj =
{
  "name":["Temperature","",""],
  "value":[
     "50,0,0,0,0",
     "0,0,0,0,0",
     "0,0,0,0,0",
     "0,0,0,0,0",
     "0,0,0,0,0"
  ],
  "color":["RED"]
};
        
for (var i=0; i<obj.value.length; i+=1) {
    
     var arr = obj.value[i].split(/,/);

     // Convert strings to numbers
     for (var j=0; j<arr.length; j+=1) {
         arr[j] = parseInt(arr[j]);
     }

     obj.value[i] = arr;
}

var data = obj.value;

// This is an RGraph function that logs variables to the
// browser console
$cl(data);


And if you are getting a getContext error - you're probably using the wrong ID (of the canvas) in your constructor.

Richard

Add a reply




« Back to message list