Java-Google Charts API JSON无效
作者:互联网
以下代码为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