2010-12-18 70 views
0

我是使用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

回答

3

問題是你所顯示的不是一個有效的JSON對象。

您是否嘗試驗證您嘗試使用的JSON對象?我在http://www.jsonlint.com/http://jsonformatter.curiousconcept.com/上驗證了它。

他們會告訴你在你的JSON對象中的錯誤。

如果你看看http://json.org/中的文檔,它們會告訴你每一個字符串都需要用「string」作爲引號。你的例子並沒有遵循這一點。您的最後一個對象之後還有一個尾隨逗號。

這是你的實際JSON會是什麼樣

{ 
    "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]] 

    } 

} 

現在,如果你想分析或創建的Android JSON對象和數組,there are four convenient classes which are very useful.

您可以使用那些做你想做的任何操作在實際的JSON對象上做。他們將始終遵循JSON準則。

要在您的示例中獲取標籤,您必須在您的json對象上使用JSONObject.getString(「label」)。

這裏是another example.

+0

感謝後近六小時,我終於得到了我想要的東西:)從來沒有想過它確認的。因爲它工作:)我會發布我的解決方案。 – Stevanicus 2010-12-18 18:51:17