ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java-Google Charts API JSON无效

2019-10-30 19:01:36  阅读:182  来源: 互联网

标签:google-visualization json java


以下代码为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对象字符串化为JSON的方法.

看看this问题的答案.它可能会帮助您.

标签:google-visualization,json,java
来源: https://codeday.me/bug/20191030/1970102.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有