grails JSON绑定到LinkedHashSet而不是JSONArray以获得深度嵌套关系 [英] grails JSON binding to LinkedHashSet instead of JSONArray for deeply nested relation
问题描述
Group - >区域 - >段
(1) - > (n)→> (n)
在我的命令对象中:
class GroupCommand {
Long id
设置区域
}
当绑定JSON请求时,zone正确绑定,我得到一个LinkedHashSet,我可以获取属性并与我的域对象一起使用。但是,当我开始迭代服务中的段时:
groupCommand.zones.each {zone - >
zone.segments.each {segment - >
//想要在这里得到LinkedHashMap也是
//但是得到JSONArray
}
}
如上所述,我理想地喜欢深度嵌套的Segments绑定到LinkedHashMap,但它绑定到JSONArray。
<任何建议如何让它绑定到一个LinkedHashMap,因为我想避免在我的服务中操纵JSONArray,从而将我的服务与JSON格式耦合在一起。
如果有一种方法可以在命令级使用getter进行转换,那么我也是这样。
谢谢
编辑:
使用
列出区域= org.apache.commons.collections.list.LazyList.decorate(new ArrayList(),new org.apache.commons.collections.functors.InstantiateFactory(ZoneCommand.class))
似乎可以工作,但底层对象仍然是JSON元素。然后我尝试使用:
List< RateZoneCommand>分区= org.apache.commons.collections.list.LazyList.decorate(新的ArrayList(),新的org.apache.commons.collections.functors.InstantiateFactory(ZoneCommand.class))
至少我收到了一个错误,表明它正在尝试转换:
验证错误:... org.codehaus.groovy.grails.web.json.JSONArray为属性区域所需的类型java.util.List;嵌套异常是java.lang.IllegalStateException:无法将[org..JSONObject]类型的值转换为属性区域[0]的所需类型[ZoneCommand]:找不到匹配的编辑器或转换策略。
为每个级别创建一个命令类。标记区域
- 和段
- 命令为 @Validateable
。
到您的 GroupCommand
,请添加:
List zones = org.apache.commons.collections.list.LazyList.decorate(new ArrayList(),new org.apache.commons.collections.functors.InstantiateFactory(ZoneCommand.class))
至您的 ZoneCommand
,请添加:
List segments = org.apache.commons.collections.list.LazyList.decorate(new ArrayList(),new org.apache.commons.collections .functors.InstantiateFactory(SegmentCommand.class))
在表单中使用 group.zones [0] .segments [0]
。如果您更改了命令类的字段类型,请记住重新启动grails服务器。
I have three levels deep of a hierarchy that I am binding in a JSON request:
Group -> Zone -> Segment
(1) -> (n) -> (n)
In my command object I have:
class GroupCommand {
Long id
Set zones
}
When binding the JSON request the zones get bound properly and I get a LinkedHashSet that I can get the properties of and use with my domain object. However when I get to iterating over the segments in my service:
groupCommand.zones.each { zone ->
zone.segments.each { segment ->
//Would like to get LinkedHashMap here also
//but get JSONArray
}
}
As noted above, I'd ideally like the deeply nested Segments to also bind to a LinkedHashMap but it's bound to a JSONArray.
Any suggestions how to get it bound to a LinkedHashMap as I'd like to avoid having to manipulate JSONArray in my service and thereby coupling my service with the JSON format.
If there's a way to do the conversion at the command level using a getter I'm all for that also.
thanks
EDIT:
Using
List zones = org.apache.commons.collections.list.LazyList.decorate(new ArrayList(), new org.apache.commons.collections.functors.InstantiateFactory(ZoneCommand.class))
appears to work but the underlying objects are still JSON elements. I then tried using:
List<RateZoneCommand> zones = org.apache.commons.collections.list.LazyList.decorate(new ArrayList(), new org.apache.commons.collections.functors.InstantiateFactory(ZoneCommand.class))
and at least I got an error indicating it trying to convert:
Validation error: ... org.codehaus.groovy.grails.web.json.JSONArray to required type java.util.List for property zones; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org..JSONObject] to required type [ZoneCommand] for property zones[0]: no matching editors or conversion strategy found.
Create a command class for each level. Mark Zone
- and Segment
-command as @Validateable
.
To your GroupCommand
, add:
List zones = org.apache.commons.collections.list.LazyList.decorate(new ArrayList(), new org.apache.commons.collections.functors.InstantiateFactory(ZoneCommand.class))
To your ZoneCommand
, add:
List segments = org.apache.commons.collections.list.LazyList.decorate(new ArrayList(), new org.apache.commons.collections.functors.InstantiateFactory(SegmentCommand.class))
In your form just use group.zones[0].segments[0]
. If you change a field type of your command class, remember to restart the grails server.
这篇关于grails JSON绑定到LinkedHashSet而不是JSONArray以获得深度嵌套关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!