在grails中渲染JSON [英] Rendering JSON in grails

查看:243
本文介绍了在grails中渲染JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  render(contentType:text / json){ 
results = array {
db.eachRow(query){row - >
def rs = row.toRowResult()
def a = b(rs.name,c,d)
aMap.put(A,a)
pair(aMap )
}
}
if(results){
status =OK
}
else {
status =Nothing present




$ b $ p
$ b

以上代码以下列格式生成JSON p>

  {
results:[
{A:value1},
{A:value2},
...................
{A:valuen}
],
status:OK
}

上面,数据呈现为对象数组。有没有办法将结果数据呈现为元素数组。像

  {
results:[
value1,
value2 ,
...................
估值
],
状态:确定


解决方案

JSON对象的构建方式很模糊。我喜欢在grails中呈现JSON响应的方法是在groovy中创建一个映射或列表,然后使用 render 方法将其转换为JSON。



在render方法中进行 rowResult 的转换使其变得非常混乱,我宁愿去做这样的事情 p>

  def results = db.rows(query).collect {rowResult  - > 
b(rowResult.name,c,d)
}
render(contentType:'text / json'){[$ b $'results':results,
'status ':结果? OK:Nothing present
]}

我认为它更具可读性,甚至更短。这段代码可以得到你想要的结果:结果数组内没有对象,只是字符串。



rows ,它返回RowResult的列表,无需从ResultSet中获取它。该列表用于通过对每行的名称调用 b 来收集转换值 a 。收集元素并不意味着创建一个映射(就像你在获得的 {A:value1} JSON),只是获得相同的@ will-buck在新的空列表上使用< 运算符。



我们所做的所有渲染方法是声明 text / json 内容类型并传递一个包含关键字 results 和的文字图,状态,您要写入响应。条件运算符用于简明确定状态。它也可以像这样使用,通过JSON转换器@ will-buck也提到:

  def responseData = [
'results':results,
'status':results? OK:Nothing present
]
将responseData呈现为JSON


I use the following code to render data in JSON format.

render(contentType:"text/json") {
    results = array {
        db.eachRow(query) { row ->
            def rs = row.toRowResult()
            def a = b(rs.name,c,d)
            aMap.put("A",a) 
            pair(aMap)
        }
    }
    if (results) {
        status = "OK"
    }
    else {
        status ="Nothing present"
    }
}

The above code generates JSON in the following format

{
    "results": [
        {"A":"value1"},
        {"A":"value2"},
        ...................
        {"A":"valuen"}
    ],
    "status":"OK"
}

As u see above, the data is rendered as an array of objects. Is there a way I can render the results data as an array of elements. Like

{
    "results": [
        "value1",
        "value2",
        ...................
        "valuen"
    ],
   "status":"OK"
}

解决方案

The way the JSON object is being built is quite obscure. What I like doing to render JSON responses in grails is creating a map or list in groovy and then use the render method just to convert it to JSON.

Doing the transformation of the rowResult's inside the render method makes it quite confusing, I'd rather go for something like this

def results = db.rows(query).collect { rowResult ->
    b(rowResult.name, c, d) 
}
render(contentType: 'text/json') {[
    'results': results,
    'status': results ? "OK" : "Nothing present"
]}

I think it's more readable, and even shorter. This snippet gets you the desired result: no objects inside the results array, just strings.

Note the use of rows, which returns a list of RowResult's, eliminating the need to get it from the ResultSet's. The list is used to collect the transformed value a by calling b on each row's name. Collecting the elements doesn't imply creating a map (like in the { "A":"value1"} JSON you were getting), just the same @will-buck achieved with the << operator on a new, empty list.

All we do with the render method is declaring the text/json content type and passing it a literal map containing the keys results and status, which you want to write to the response. The conditional operator is used to concisely determine the status. It could also be used like this, by means of the JSON converter @will-buck also mentioned:

def responseData = [
    'results': results,
    'status': results ? "OK" : "Nothing present"
]
render responseData as JSON

这篇关于在grails中渲染JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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