【问题标题】:Android and java JSONAndroid 和 java JSON
【发布时间】: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


    【解决方案1】:

    问题是您显示的不是有效的 JSON 对象。

    您是否尝试验证您尝试使用的 JSON 对象?我在http://www.jsonlint.com/http://jsonformatter.curiousconcept.com/ 上验证了它。

    它们会向您显示 JSON 对象中的错误。

    如果您查看http://json.org/ 中的文档,他们会告诉您每个字符串都需要用引号括起来,例如“字符串”。你的例子不遵循这一点。最后一个对象后面还有一个逗号。

    这是您的实际 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.

    【讨论】:

    • 感谢将近六个小时后,我终于得到了我想要的东西 :) 从来没有想过要验证它。因为它有效:) 我会发布我的解决方案。
    【解决方案2】:

    查看Gson。它有自己的序列化器和反序列化器Gson User Guide

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-13
      • 2019-04-14
      • 2017-02-23
      相关资源
      最近更新 更多