问题描述
JSON
和jQuery
的新手,但这是我要尝试的工作-我将JSON
字符串作为较大报告模型的一部分返回到我的网页,以便创建多个生成报告的同时绘制图形(使用jqPlot
).
Very new to both JSON
and jQuery
, but here is what I am trying to do - I am returning a JSON
string to my webpage as part of a bigger report model in order to create a number of graphs (using jqPlot
) at the same time the report is generated.
要使用这些图表进行练习,我使用了以下教程-教程
To practice with these charts, I was using the following tutorial - Tutorial
这是本教程中的jQuery
-
jQuery(document).ready(function() {
urlDataJSON = '/Graph/HearthRateDataJSON';
$.getJSON(urlDataJSON, "", function(data) {
var dataLines = [];
var dataLabels = "";
$.each(data, function(entryindex, entry) {
dataLines.push(entry['Serie']);
dataLabels = dataLabels + entry['Name'];
});
Plot(dataLines, dataLabels);
});
});
function Plot(dataLines, dataLabels) {
var line1 = "{ label: 'line1.0' }";
options = {
legend: { show: true },
title: 'Heart rate overview',
axesDefaults: { pad: 1 },
seriesDefaults: { showMarker: false, trendline: { show: false }, lineWidth: 3 },
axes: {
yaxis: { min: 0, autoscale: true, label: 'HR[bpm]', labelRenderer: $.jqplot.CanvasAxisLabelRenderer },
xaxis: { autoscale: true, label: 'Time', labelRenderer: $.jqplot.CanvasAxisLabelRenderer }
}
};
//Data from database is already an array!
plot = $.jqplot('chartdiv', dataLines, options);
plot.redraw(); // gets rid of previous axis tick markers
}
要检索JSON
数据,本教程使用getJson()方法指向链接.我已经准备好将JSON
字符串传递给jQuery
,例如
To retrieve the JSON
data, this tutorial uses the getJson() method to point to a link. I have the JSON
string ready to pass through to the jQuery
however, e.g.
[
{"Name":"Patient","Serie":[[1,4],[2,25],[3,7],[4,14],[5,13]]},
{"Name":"Test","Serie":[[1,13],[2,5],[3,7],[4,20],[5,17]]}
]
在jqPlot
示例中,它们通过如下方式传递硬编码数据:
In the jqPlot
examples, they pass hardcoded data through as follows:
$(document).ready(function(){
var plot1 = $.jqplot ('chart1', [[3,7,9,1,4,6,8,2,5]]);
});
但是,我自动生成的JSON
字符串带有花括号等.是否可以在不使用getJson方法的情况下将其传递给jQuery
?
However, my automatically generated JSON
string has the curly braces, etc. Is there a way to pass this through to jQuery
without using the getJson method?
EDIT-饼图图表查询代码
EDIT-Pie Chart Jquery Code
$(document).ready(function () {
var data4 = '[{ "Label": "Car", "Value": 9 },{ "Label": "Bus", "Value": 2 },{ "Label": "Train", "Value": 7 },{ "Label": "Plane", "Value": 8 },{ "Label": "Boat", "Value": 4 }]';
var data = $.parseJSON(data4);
var plot1 = jQuery.jqplot('pieChart', [data],
{
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' }
}
);
});
推荐答案
您可以使用其他答案中提供的任何建议方法来解析它,然后使用它.或者只是简单地使用您拥有的JSON
数据,因为它是一个数组,并且所有在{}
中的数据都被视为映射.因此,您可以直接使用它们,而无需进行任何解析,就像我在此处可用代码所示的类似情况下所做的那样.
You could parse it with any suggested methods provided in other answers and then using it. Or just simply use the JSON
data you have as it is an array and all in {}
treat as maps. Therefore, you can use them straight without any parsing as I do in a similar situation shown in the code available here.
在提供的示例中,有JSON
个数据编码为名为json的变量.
In the sample provided there is JSON
data encoded in variable called json.
var json = [{
"Name": "Poll Results",
"Serie": [[2, 5, 4], [4, 1, 7], [6, 3, 1], [3, 4, 2]]}];
var dataLines = json[0].Serie;
var title = json[0].Name;
然后将dataLines
变量末尾传递给图表.
Then at the end dataLines
variable is passed to the chart.
编辑
在您提供了样本之后,我得出以下结论.显然,给定的数据不是jQuery的parseJSON方法可以使用的JSON格式.但是,当您字符串化时,它会起作用.
After playing with the sample you provided I came to the following conclusions.Apparently the given data is not a format of JSON that the parseJSON method of jQuery can work with. But when you stringify it works.
我的示例显示,在对数据应用stringify后,它可以正常工作.
我尝试通过将其包装在''
中并将其转换为与JSON等效的String,并且可以正常工作.因此,答案很容易使用parseJSON
方法,您必须向其传递一个String而不是任何其他种类的Object.
I tried and turned your JSON into a its String equivalent by wrapping it inside ''
and it worked. So the answer is simple to use parseJSON
method you must pass it a String and not an Object of any other sort.
这篇关于不使用getJson()将Json字符串传递给JQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!