我有一个从Groovy的HTTPBuilder返回的JSON对象.JSON包含一些表示为JSONNull对象的空值.问题是当我尝试在响应中渲染JSON时,我在尝试渲染JSONNull时出现错误.我得到的回复只是部分呈现的.我希望它呈现为"null".我该怎么做呢?
码:
render(contentType: "text/json") {
listOfJSONObjectsThatIncludeJSONNulls
}
Run Code Online (Sandbox Code Playgroud)
错误:
| Error 2013-09-17 11:33:56,965 [http-bio-8080-exec-4] ERROR errors.GrailsExceptionResolver - JSONException occurred when processing request: [GET] /my/action
Object is null. Stacktrace follows:
Message: Object is null
Line | Method
->> 69 | isEmpty in net.sf.json.JSONNull
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 199 | …Run Code Online (Sandbox Code Playgroud) 我有一个地图,其中一个键包含多个值
datamap = [ 'Antenna Software':[ 'Salarpuria', 'Cessna', 'Vrindavan Tech', 'Alpha Center' ],
'Ellucian':[ 'Malvern', 'Ellucian House', 'Residency Road'] ]
Run Code Online (Sandbox Code Playgroud)
在这里,我需要按字母顺序对值进行排序
datamap = [ 'Antenna Software':[ 'Alpha Center', 'Cessna', 'Salarpuria', 'Vrindavan Tech' ],
'Ellucian':[ 'Ellucian House', 'Malvern', 'Residency Road' ] ]
Run Code Online (Sandbox Code Playgroud)
如何以时髦的方式做到这一点?
我的东西看起来像是类似的规格:
def "my spec"(Record record) {
given:
Something something = getSomething()
and:
otherThing = getOtherThing()
doFlow(something, record)
if (record.someType = Types.SOME_SPECIFIC_TYPE) {
doFlow(something, record)
}
}
def doFlow(Something something, Record record) {
when:
//code
then:
//asserts
when:
//code
and:
//mode code
then:
//code
}
Run Code Online (Sandbox Code Playgroud)
但是,在运行时,我得到:groovy.lang.MissingMethodException: No signature of method doFlow() is applicable for arguments Something, Record values: [given values].
我有这个
"/"{controller = "normal"
action = "index"
}
Run Code Online (Sandbox Code Playgroud)
这将在我给出时发挥作用localhost:8080,我需要的是我必须通过这个发送一个参数,以便请求开始/将进入指定的控制器和该参数的动作,所以相应地我可以做一些条件来放置一些功能我的看法.所以来自该映射的请求必须发送一些参数,该参数不应该改变我localhost:8080使用该参数的URL .
在使用Groovy In Action的Groovy处理Java Bean的部分中,我找到了此脚本(稍作修改):
class Book{
String title
}
def groovyBook = new Book()
// explicit way
groovyBook.setTitle('What the heck, really ?')
println groovyBook.getTitle()
// short-hand way
groovyBook.title = 'I am so confused'
println groovyBook.title
Run Code Online (Sandbox Code Playgroud)
该类中没有此类方法,Book那么它如何工作?
我有这个代码来分割线来获取键值对,如果值丢失则抛出执行.
为什么要施行?拆分此键值对线的正确方法是什么.
def lline="name="
def (key, value) = lline.split("=")
Run Code Online (Sandbox Code Playgroud)
错误:
Caught: java.lang.ArrayIndexOutOfBoundsException: 1
java.lang.ArrayIndexOutOfBoundsException: 1
Run Code Online (Sandbox Code Playgroud)
谢谢
有没有人有任何样本Groovy代码将JSON文档转换为CSV文件?我试图在Google上搜索但无济于事.
示例输入(来自评论):
[ company_id: '1',
web_address: 'vodafone.com/',
phone: '+44 11111',
fax: '',
email: '',
addresses: [
[ type: "office",
street_address: "Vodafone House, The Connection",
zip_code: "RG14 2FN",
geo: [ lat: 51.4145, lng: 1.318385 ] ]
],
number_of_employees: 91272,
naics: [
primary: [
"517210": "Wireless Telecommunications Carriers (except Satellite)" ],
secondary: [
"517110": "Wired Telecommunications Carriers",
"517919": "Internet Service Providers",
"518210": "Web Hosting"
]
]
Run Code Online (Sandbox Code Playgroud)
更多来自编辑的信息:
def export(){
def exportCsv = [ [ id:'1', color:'red', planet:'mars', description:'Mars, the "red" planet'],
[ …Run Code Online (Sandbox Code Playgroud) 错误| 服务器无法启动端口8080:地址已在使用中:JVM_Bind(使用--stacktrace查看完整跟踪)
当我第二次使用--- 2 Grails Command(run-app)执行我的grails项目时,每次都会遇到这种类型的错误.
但是当我使用任务管理器杀死javaw.exe时,如果我再次运行它对我有效.这有什么永久的解决方案吗?..请建议.
|包装Grails应用..
|编译10个源文件..
|编译121个源文件.......
|编译9个源文件.........................
|运行Grails应用程序
错误| 服务器无法启动端口8080:地址已在使用中:JVM_Bind(使用--stacktrace查看完整跟踪)
我试图在Groovy中预测此代码的行为
userList.find { }
Run Code Online (Sandbox Code Playgroud)
当find调用该方法并传递一个闭包时,它返回第一个评估闭包的元素,使其理解为Groovies true.当find没有任何参数调用该方法时,它返回列表中true根据Groovy真值匹配的第一个对象.
如果使用空闭包会发生什么?
null返回?.find()?根据gradle文档/第13.5.2节,我们可以省略方法调用中的括号:
括号是方法调用的可选项.
但是当我们尝试应用java插件时它似乎不起作用.如果脚本包含以下行:
apply [plugin: 'java']
Run Code Online (Sandbox Code Playgroud)
我们会收到错误:
Maybe something should be set in parentheses or a comma is missing?
@ line 1, column 8.
apply [plugin: 'java']
^
Run Code Online (Sandbox Code Playgroud)
但是如果我们把这个Map-literal放到括号中它就可以了.
apply([plugin: 'java'])
Run Code Online (Sandbox Code Playgroud)
因此,当参数为a时Map,我们不能省略括号,是吗?