【发布时间】:2010-12-18 16:55:28
【问题描述】:
我是使用 JSON 的新手,目前正在使用 FLOT 图表来通过 WebView 显示图表。
http://rapidandroid.org/wiki/Graphing
但是,当涉及到 JSON 解析时,我无法将我的数据表示为 JavaScript 等价物。我已经玩了几个小时了,但我无法让我的 JSON 输出如下所示。
var datasets = {
"Symptoms": {
label: "Symptoms",
data: [[1988, 10], [1989, 20], [1990, 10], [1991, 30], [1992, 40]]
},
"Reactions": {
label: "Reactions",
data: [[1988, 0], [1989, 0], [1990, 10], [1991, 30], [1992, 40]]
},
"Injections": {
label: "Injections",
data: [[1988, 0], [1989, 0], [1990, 0], [1991, 30], [1992, 40]]
},
};
使用 davids 解释浮动图 http://w2davids.wordpress.com/android-charts-the-html5-and-javascript-way/
提前致谢。
附:我得到的最接近的是: 字符串数据= "[0,1],[5,3],[5,6],[7,2],[10,12]"; arr.put(数据);
提出的问题是: {"data":["[0,1],[5,3],[5,6],[7,2],[10,12]"]} //我怎样才能摆脱.." .. 我如何才能在 JSON 对象中获得 label:?也没有引号
解决方案
将 json 对象从 android 传递到 javascript(即 webview) 适合使用 flot javascript 库显示的任何人 android中的图表
Java 活动
String json = "{"
+ " \"Symptoms\":"
+ "{ \"label\": \"Symptoms\","
+ " \"data\": [[1988, 10], [1989, 20], [1990, 10], [1991, 30], [1992, 40]] "
+ "},"
+ " \"Infections\":"
+ "{ \"label\": \"Infections\","
+ " \"data\": [[1988, 0], [1989, 0], [1990, 10], [1991, 35], [1992, 28]] "
+ "}"
+ "}";
mAppView.loadUrl("javascript:GotGraph('"+json+"');");
Javascript 端(FLOT)(允许多行
function GotGraph(data) {
var datasets = JSON.parse(data);
// hard-code color indices to prevent them from shifting as
// countries are turned on/off
var i = 0;
$.each(datasets, function(key, val) {
val.color = i;
++i;
});
// insert checkboxes
var choiceContainer = $("#choices");
$.each(datasets, function(key, val) {
choiceContainer.append('<br/><input type="checkbox" name="' + key +
'" checked="checked" id="id' + key + '">' +
'<label for="id' + key + '">'
+ val.label + '</label>');
});
choiceContainer.find("input").click(plotAccordingToChoices);
//
function plotAccordingToChoices() {
var data = [];
choiceContainer.find("input:checked").each(function () {
var key = $(this).attr("name");
if (key && datasets[key])
data.push(datasets[key]);
});
if (data.length > 0)
$.plot($("#placeholder"), data, {
yaxis: { min: 0 },
xaxis: { tickDecimals: 0 }
});
}
plotAccordingToChoices();
}
感谢
http://rapidandroid.org/wiki/Graphing
http://w2davids.wordpress.com/android-charts-the-html5-and-javascript-way/
和@achie
【问题讨论】:
标签: java android json graph webview