2015-11-06 54 views
0

我想呈现包含缓存JSON的字段的JSON。我怎样才能做到这一点?Groovy/Grails - 在JSON中渲染缓存的JSON

实施例:

def cachedJSON = [cachedPropertyOne:'a', cachedPropertyTwo:'b'] as JSON 
render([listEntryOne:'c', listEntryTwo:'d', cachedProperties: cachedJSON] as JSON) 

与上面的代码的问题是,cachedProperties被转义。例如,"变成\"

回答

0

根据我的经验,渲染为JSON不喜欢在线地图,所以我总是声明一个变量,是这样的:

def result = [:] 
// assign values to result 
render result as JSON 

它按预期工作的方式。

0

在您提供的代码中,cachedJSON只是一个映射,所以我假设它是json语法中的字符串的替代品。

例如,如果cachedJSON居然是:

def cachedJSON = '{"cachedPropertyOne":"a","cachedPropertyTwo":"b"}' 

然后,你可以使用JSON类的parse方法将其转换为JSONObject然后可以在渲染的地图被放回。

def cachedJSON = '{"cachedPropertyOne":"a","cachedPropertyTwo":"b"}' 

render([ 
    listEntryOne:'c', 
    listEntryTwo:'d', 
    cachedProperties: JSON.parse(cachedJSON) 
] as JSON) 
+0

你的假设是正确的 - 我编辑了我的文章。不过,我希望有更高效的方式去实现它。 – Anonymous1