java - Google 图表 API JSON 无效

标签 java json google-visualization

以下代码为 Google 图表返回 json

public static DataTable generateDataTable(){
        // Create a data table,
        DataTable data = new DataTable();
        ArrayList cd = new ArrayList();
        cd.add(new ColumnDescription("name", ValueType.TEXT, "Animal name"));
        cd.add(new ColumnDescription("link", ValueType.TEXT, "Link to wikipedia"));
        cd.add(new ColumnDescription("population", ValueType.NUMBER, "Population size"));
        cd.add(new ColumnDescription("vegeterian", ValueType.BOOLEAN, "Vegetarian?"));

        data.addColumns(cd);

        // Fill the data table.
        try {
          data.addRowFromValues("Aye-aye", "http://en.wikipedia.org/wiki/Aye-aye", 100, true);
          data.addRowFromValues("Sloth", "http://en.wikipedia.org/wiki/Sloth", 300, true);
          data.addRowFromValues("Leopard", "http://en.wikipedia.org/wiki/Leopard", 50, false);
          data.addRowFromValues("Tiger", "http://en.wikipedia.org/wiki/Tiger", 80, false);
        } catch (TypeMismatchException e) {
          System.out.println("Invalid type!");
        }


        return data;
}

public static void main(String []args){
    String json = JsonRenderer.renderDataTable(generateDataTable(), false, false).toString();
    System.out.println(json);
}

但是返回的JSON

{cols:[{id:'name',label:'Animal name',type:'string',pattern:''},{id:'link',label:'Link to wikipedia',type:'string',pattern:''},{id:'population',label:'Population size',type:'number',pattern:''},{id:'vegeterian',label:'Vegetarian?',type:'boolean',pattern:''}]}

粘贴到 http://jsonlint.com/ 时无效

此外,返回的 json 中显示的模式值是什么 {id:'name',label:'Animal name',type:'string',**pattern:''**}

最佳答案

Google 向您发送的是 JavaScript 对象 而不是 JSON

您需要想出一种方法将 JavaScript 对象 stringfy 转换为 JSON

查看 this 的答案问题。它可能对您有所帮助。

关于java - Google 图表 API JSON 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17338978/

相关文章:

java - 无法解析 javax.media.nativewindow.Capability 类型。它是从所需的 .class 文件间接引用的

java - android.util.Pair 示例

java - 如何用java检查mysql中的id是否已经存在

json - 用于 json 自动的 sql 服务器。如何获得所有结果

mysql - 谷歌图表 MySQL 结果

java - 是否有正确的方法让 util 方法找到测试资源路径?

javascript - 使用javascript将base64字符串转换为图像

charts - 这在谷歌图表中是可能的还是有替代的黑客攻击

charts - javascript google 可视化选择选项

javascript - 树递归: How to get the parent root of the selected tree node