gpt4 book ai didi

java - Google 图表 API JSON 无效

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:17:36 24 4
gpt4 key购买 nike

以下代码为 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com