使用条件运算符在Grails中渲染为'JSON'不会正确渲染 [英] Rendering 'as JSON' in Grails with conditional operator doesn't render correctly

查看:85
本文介绍了使用条件运算符在Grails中渲染为'JSON'不会正确渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天遇到了这个奇怪的结果,试图在Grails 2.0.4中将对象列表呈现为JSON ...(我知道我会后悔因为鼻子下面的东西而提出这个问题...... 更新 5月26日,我的预测是正确的,见下面: - ))



这工作正常; JSON在浏览器中正确呈现......

  def products = [] //服务的产品对象的ArrayList 
def model =(产品)? [产品:产品]:[产品:未找到产品]
将模型呈现为JSON



<为什么没有 model 的这个缩短版本没有工作?

  def products = [] 
render((products)?[products:products]:[products:No产品找到])作为JSON

以上代码生成的JSON输出为单行文本,所以我怀疑它没有选择作为JSON ,但它的括号正确,所以交易是什么?


['products':[com.test.domain.Product:null,
com.test.domain.Product ...]



解决方案

这是 render 的正常行为。当您为 render 提供参数而不使用大括号如$ / $>
$ b

将模型渲染为JSON $ b

它将隐含的调整设置 content-type 文本/ JSON 。但是在后面的例子中,你已经在不知不觉中使用 render 来使用大括号,例如[ render 使渲染使用正常 render()]



render((products)? [产品:产品]:[产品:未找到产品])为JSON

在上述情况下,您必须通过在 render 中提到 contentType text model status 等等。所以,为了在浏览器/视图中将内联控制逻辑呈现为JSON,您必须如下所示:

  render(contentType:application / json,text:[products:(products?:No products找到)]为JSON)

您也可以使用 content-type 作为 text / json 。我更喜欢 application / json



UPDATE

替代最简单的方法:

render([products:(products?:No products found)] as JSON)

Came across this strange result today trying to render a list of objects as JSON in Grails 2.0.4...(i know i'm gonna regret asking this on account of something right under my nose...updated 5/26, my prediction was correct, see below :-))

This works fine; the JSON renders correctly in the browser...

def products = [] //ArrayList of Product objects from service       
def model = (products) ? [products:products] : [products:"No products found"] 
render model as JSON

..so why doesn't this shortened version without model work?

def products = []       
render ((products) ? [products:products] : [products:"No products found"]) as JSON

The resulting JSON from the above code is output as a single line of text, so I suspect it's not picking up as JSON, but it's parenthesized correctly, so what's the deal?

['products':[com.test.domain.Product : null, com.test.domain.Product...]

解决方案

This is a normal behavior of render. When you provide arguments to render without braces like

render model as JSON

It makes an implicit adjustment setting up the content-type to text/json. But in the later case, you have unknowingly made the render to use the braces like [mark on the first brace after render makes render use the normal render()]

render ((products) ? [products:products] : [products:"No products found"]) as JSON.

In the above case, you have to pass in the named parameters to render mentioning the contentType, text or model, status etc. So in order to render the inline control logic as JSON in browser/view you have to do like below:

render(contentType: "application/json", text: [products: (products ?: "No products found")] as JSON)

You can also use content-type as text/json. I prefer application/json.

UPDATE
Alternative Simplest Way:
render([products: (products ?: "No products found")] as JSON)

这篇关于使用条件运算符在Grails中渲染为'JSON'不会正确渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆