I have to to draw a chart from the following json string:
{
"d":{
"results":[
{
"_metadata":{
"uri": "http://www.something.com/hi",
"type" : "something.preview.hi"
}, "Period", "1988", "Age": 20, "Size" : 80
},
"_metadata":{
"uri": "http://www.something.com/hi",
"type" : "something.preview.hi"
}, "Period", "1989", "Age": 25, "Size" : 90
}
] }}
I use jquery.flot.js library to draw the chart. Here is the example to draw the chart and it works fine:
var d1 = [];
for (var i = 0; i < 14; i += 0.5)
d1.push([i, Math.sin(i)]);
var d2 = [[0, 3], [4, 8], [8, 5], [9, 13]];
myChart.setDataArray(d1);
myChart.setDataArray(d2);
According to example I have converted the json to an array of object:
var results = $.parseJSON(responseText)["d"]["results"];
var sampleData = [];
for (var i = 0, len = results.length; i < len; i++) {
var result = results[i];
sampleData.push({ Perioden: result.Perioden,
Size: result.Size,
Age: result.Age});
}
var myData = [];
for (var i in sampleData) {
myData.push([sampleData[i].Period, sampleData[i].Age]);
}
var myData1 = [];
for (var i in sampleData) {
myData1.push([sampleData[i].Period, sampleData[i].Size]);
}
myChart.setDataArray(myData);
myChart.setDataArray(myData1);
but I get the error that the data format is wrong for the data chart.
Can anyody see what is the different between my code and the example?
I guess that your json is well formated. but the exemple you are providing is not (trailing “i” after second “uri2 line
and “,” instead of “:” after “Period”
second thing, you have Period in your json and Perioden is your js code. check that you are using the same name everywhere
then you can try to console.log(myData, myData1) to see if you have the expected results
tell us if it’s enough or if you have another problem