【问题标题】:Passing json object from grails controller to a gsp rendering a d3 chart?将 json 对象从 grails 控制器传递给呈现 d3 图表的 gsp?
【发布时间】:2013-05-06 21:33:41
【问题描述】:

我正在使用在 grails 控制器中创建的 json 对象创建 d3 图表。目前我将 json 作为字符串传递给视图,这似乎不是最好的方法。

d3 提供了一种方法来传递带有 url 的 json 文件,例如

d3.json("https://api.github.com/repos/mbostock/d3/git/blobs/2e0e3b6305fa10c1a89d1dfd6478b1fe7bc19c1e?" + Math.random())
.on("progress", function() { 

我想通过提供控制器操作 url 来使用此方法。我想知道这是否可行,如果可以,返回类型应该是什么?一个json对象还是一个文件?我对 grails 和 d3 还很陌生,所以遇到了一些麻烦

【问题讨论】:

  • 你在controller中尝试过render as JSON吗?如果你的回复可以表示为maplist,那么你可以使用render myRespone as JSONimport grails.converter.JSON)。跨度>
  • 谢谢..这种方法有效..把这个写成答案,我会接受

标签: grails view charts controller d3.js


【解决方案1】:

控制器中的render as JSON 在这种情况下应该会有所帮助。
如果您的回复可以表示为maplist,请使用

import grails.converter.JSON

render myRespone as JSON

如果您需要以不同格式呈现响应,则可以使用withFormat 来满足目的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-07
    • 2016-02-09
    • 1970-01-01
    • 2017-01-11
    • 1970-01-01
    相关资源
    最近更新 更多