我是使用JSON的新手,目前正在使用FLOT圖形來通過WebView顯示圖形。Android和java JSON
http://rapidandroid.org/wiki/Graphing
然而,當涉及到我無法讓我的數據表示爲等價的JavaScript JSON解析。我一直在玩這個小時,我無法讓我的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]]
},
};
提前 http://w2davids.wordpress.com/android-charts-the-html5-and-javascript-way/
由於使用戴維斯的海軍報圖的解釋。
附:我得到的最接近的是: String data =「[0,1],[5,3],[5,6],[7,2],[10,12]」; arr.put(data);
問題出在: {「data」:[「[0,1],[5,3],[5,6],[7,2],[10,12]」 ]} //我怎樣才能擺脫..「.. 還我怎麼能得到標籤:在JSON對象還沒有引號
解決方案
要傳遞從android到javascript(即webview)的json對象 適合任何使用flot javascript庫顯示 android
爪哇活動
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/
and @achie
感謝後近六小時,我終於得到了我想要的東西:)從來沒有想過它確認的。因爲它工作:)我會發布我的解決方案。 – Stevanicus 2010-12-18 18:51:17