如何将JSONObject地图转换为Map?

时间:2014-06-09 12:09:31

标签: json grails groovy

我从以下示例派生而来 http://mrhaki.blogspot.de/2013/12/grails-goodness-rendering-partial.html

class Book {
    String title
    Map    details
}

控制器

@Transactional
class BookApiController extends RestfulController {

    static responseFormats = ['json', 'xml']

    BookApiController() {
        super(Book)
    }

    @Override
    @Transactional
    def save(Book bookInstance) {

        println "details: " + bookInstance.details.getClass().name

        bookInstance.save flush:true

        respond bookInstance
    }

}

URLMappings

"/api/book"(resources: "bookApi")

当我使用此curl调用时:

curl -i -X POST -H "Content-Type: application/json" -d '{"title":"Grails in Action","details":{"isbn":"123456789"}}' http://localhost:8080/restpoint/api/book

Grails抛出以下异常:

details: org.codehaus.groovy.grails.web.binding.bindingsource.JsonDataBindingSourceCreator$JsonObjectMap

| Error 2014-06-09 13:45:52,057 [http-bio-8080-exec-4] ERROR errors.GrailsExceptionResolver  - ClassCastException occurred when processing request: [POST] /restpoint/api/book
com.google.gson.JsonPrimitive cannot be cast to java.lang.String. Stacktrace follows:
Message: com.google.gson.JsonPrimitive cannot be cast to java.lang.String
    Line | Method
->>   23 | $tt__save in sample.core.BookApiController
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    198 | doFilter  in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter  in grails.plugin.cache.web.filter.AbstractFilter
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    724 | run       in java.lang.Thread

我需要在我的图书域中使用map属性(此处称为'详细信息')这是前提条件。 我们可以在地图上方看到它是 org.codehaus.groovy.grails.web.binding.bindingsource.JsonDataBindingSourceCreator $ JsonObjectMap

的一个实例

如何将其投射到地图中?

有什么想法吗?

提前致谢。

-markus

1 个答案:

答案 0 :(得分:0)

这似乎是一个错误,请在http://jira.grails.org/browse/GRAILS

报告JIRA问题