[英]How to get data from JSON array JavaScript
I want to push the data from piedatalist which contains two attributes, value and key and I want to pass to jqplot like this [key,value] to render the chart but it doesn't work.我想从包含两个属性值和键的 piedatalist 推送数据,我想像这样传递给 jqplot [key,value] 以呈现图表,但它不起作用。
$(document).ready(function () {
var json = JSON.parse("[{"pieDataList":
[{"value": 100, "key": "non"}],
"titre": "fghjklcom", "nbreReponse": 1}, {
"pieDataList": [{"value": 100, "key": "non"}],
"titre": "fghjklcom", "nbreReponse": 1}]");
// $.each(json, function (index,ch) {
for (i in json) {
var value = [];
//get the map
for (j = 0; j < json[i].PieDataList.length; j++) {
//iterate each item of list
value.push([json[i].PieDataList[j].key,
json[i].PieDataList[j].value]);
}
var plot1 = jQuery.jqplot('ch', [value],
{
seriesDefaults: {
// Make this a pie chart.
renderer: jQuery.jqplot.PieRenderer,
rendererOptions: {
// Put data labels on the pie slices.
// By default, labels show the percentage of the
slice.
showDataLabels: true
}
},
legend: {show: true, location: 'e'}
})
}
alert(item.PieDataList);
console.log("Titre=" + item.Titre);
});
The JSON at the start can't just be pasted into the code.开始时的 JSON 不能只是粘贴到代码中。 It's a string, but there are newlines which break strings, and double-quote characters which would end the string also.
它是一个字符串,但有换行符会中断字符串,而双引号字符也会结束字符串。
You can just paste in the JSON as a variable instead of a string and JavaScript will allow it just fine:您可以将 JSON 粘贴为变量而不是字符串,JavaScript 将允许它正常工作:
var json = [{"pieDataList":
[{"value":100,"key":"non"}],
"titre":"fghjklcom","nbreReponse":1},{"pieDataList":
[{"value":100,"key":"non"}],
"titre":"fghjklcom","nbreReponse":1}];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.