【发布时间】:2014-06-29 09:14:12
【问题描述】:
问题如下:我想处理一个带有 JSON 正文的 POST 请求。主体由 JSON 对象数组组成,无需进一步嵌套,即简单的 HashMap。所有这些对象都代表来自 Android 应用程序的 JSON 序列化域类,它们将在我的 Grails 应用程序中具有它们的对应项。我正在考虑解析 JSON 正文,遍历每个元素并将每个节点保存为其对应的域类实例。
a) 我应该如何保存实例?我对 Grails/Groovy 很陌生,所以请原谅任何重大错误。到目前为止的代码是
public static Object JSONArray2Instances(String json, Class type) {
def slurper = new JsonSlurper()
def result = slurper.parseText(json)
//we only want to parse JSON Arrays
if (!(result instanceof JSONArray))
return null
result.each {
def instance = it.asType(type)
// now I need to save to domain class!
}
}
b) 我在哪里放置相应的代码?目前它位于 /grails-app/src/groovy 中。测试去哪儿了? (因为它不是一个“真正的”Grails 组件)
c) 中间命令对象是否更合适?
【问题讨论】:
标签: json grails reflection polymorphism