将 DataTable 对象从 JavaScript 传递到 Java
我在客户端使用 Google Visualization API,并创建一个 DataTable 对象。然后我想将其传递到我的服务器并通过电子表格 API 将其上传到电子表格。可能最好的方法是使用 JSON,因此我使用 toJSON() 方法将其转换并通过 POST 发送到我的服务器。我尝试使用这两个类:
现在我注意到,这两个类不兼容,至少在 JSON 上不兼容。例如,JavaScript 类转换为:
{"cols":[
{"id":"Col1","label":"","type":"string"}
{"id":"Col2","label":"","type":"date"}
],
"rows":[
{"c":[{"v":"a"},{"v":"Date(2010,10,6)"}]},
{"c":[{"v":"b"},{"v":"Date(2010,10,7)"}]}
]
}
但是 Java 端 DataTable 的参数名称不同,而我使用的 Gson 具有不同的类型值:
cols -> columns
c -> cells
v -> value
type:"string" -> type:"TEXT"
type:"number" -> type:"NUMBER"
而且恐怕还有更多的不兼容性。
那么..如何将 JavaScript DataTable 转换为 Java 对象 DataTable?
I am using the Google Visualization API on the client side and I create a DataTable object. Then I want to pass it to my server and upload it via the Spreadsheet API to a spreadsheet. Probably the best way is to use JSON, so I converted it with the method toJSON() and sent it over POST to my server. I tried to use these 2 classes:
Now I noticed, that these 2 classes aren't compatible, at least not over JSON. The JavaScript class converts for example to this:
{"cols":[
{"id":"Col1","label":"","type":"string"}
{"id":"Col2","label":"","type":"date"}
],
"rows":[
{"c":[{"v":"a"},{"v":"Date(2010,10,6)"}]},
{"c":[{"v":"b"},{"v":"Date(2010,10,7)"}]}
]
}
But the Java side DataTable has different names for the parameters, and I am using Gson which has different type values:
cols -> columns
c -> cells
v -> value
type:"string" -> type:"TEXT"
type:"number" -> type:"NUMBER"
And I am afraid that there are even more incompatibilities.
So.. how can I convert the JavaScript DataTable to the Java object DataTable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我反过来也遇到了同样的问题。看来 Java 数据源库中的 DataTable 对象与 Google Visualization API 中的 Javascript DataTable 对象并不平行。
返回Java数据源库DataTable对象需要使用JsonRenderer,而不是默认的序列化。它似乎只适用于从服务器到客户端的传递。我不确定是否可以从另一个方向完成。
但是,默认序列化返回的 Java DataTable 对象与 Google Visualization API javascript DataTable 不同。您无法将其传递给 GVis 图表。
相反,您可以在 Java 中使用
JsonRenderer
类 (查看此 Google 网上论坛电子邮件)将其转换为缺少引号的类似 Json 的字符串围绕适度压缩的属性。该字符串可以在 Javascript 中通过用括号括起来来解析,示例中的对象文字表示法中未显示 (请参阅此 Google 网上论坛论坛):
我没有看到与 Java 数据源库 DataTable 对象相反的方法。
所以这不是一个完整的答案,但你并不孤单
I ran into the same problem in reverse. It appears that the DataTable object in the Java Datasource Library is not parallel to the Javascript DataTable object in the Google Visualization API.
Returning a Java Datasource Library DataTable object requires using the JsonRenderer, rather than the default serialization. And it appears only to work passing from the server to the client. Am not sure if it can be done the other direction.
However, the Java DataTable object returned by default serialization is not the same thing as the Google Visualization API javascript DataTable. You can't pass it to a GVis chart.
Instead, from Java, you use the
JsonRenderer
class (see this Google Groups email) to convert it to a Json-like string that's missing quotes around attributes for modest compression.That string can be parsed in Javascript by surrounding with parentheses, not shown in the object literal notation in the examples (see this Google Group forum):
I don't see a method for going the reverse way to the Java Datasource Library DataTable object.
So not quite an answer but you're not alone
好吧,我在后端使用 python,在前端使用 GWT,并将 DataTable 从后端传递到前端,没有任何问题。
我在后端使用 google-visualization-python api 来创建数据表。
解析是通过以下代码完成的:
我还将解析后的 DataTable 转换回 JSON,以将 json 字符串存储在 localStorage 中,并且解析存储的 json 字符串也可以正常工作。
GWT
DataTable
只是一个简单的包装器,最终只是通过 JSNI 调用底层 JavascriptDataTable
的函数。所以我看不出它们有任何不兼容的原因。确保您使用最新的 gwt-可视化 API (1.1.2) ?
Well I am using python on the backend and GWT on the frontend and passing a DataTable from the backend to the frontend works without any problems.
I am using the google-visualization-python api on the backend to create the DataTable.
Parsing is done with following code:
I also convert the parsed DataTable back to JSON to store the json string in localStorage and parsing the stored json string also works fine.
The GWT
DataTable
is just a simple wrapper which ultimately just calls the function of the underlying JavascriptDataTable
via JSNI.So I don't see any reason why they should be incompatible.Make sure you use the latest gwt-visualization API (1.1.2) ?