Grails:值不按顺序:写入时预期模式为OBJECT或ARRAY

时间:2016-11-22 16:25:42

标签: json grails grails3

我正在尝试使用grails.converters.JSON解析JSON对象,但会出现此错误。

代码

def str  = '{"a": "b"}'
def json = new JSON(str)

def map = [:]
map.a = "b"
def json = map as JSON
json = new JSON(json.toString())

正在返回以下错误:

2016-11-22 14:21:34.592 ERROR --- [nio-8080-exec-9] o.g.web.errors.GrailsExceptionResolver   : JSONException occurred when processing request: [GET] /test/index
Value out of sequence: expected mode to be OBJECT or ARRAY when writing '{"a":"b"}' but was INIT. Stacktrace follows:

java.lang.reflect.InvocationTargetException: null
    at org.grails.core.DefaultGrailsControllerClass$ReflectionInvoker.invoke(DefaultGrailsControllerClass.java:210)
    at org.grails.core.DefaultGrailsControllerClass.invoke(DefaultGrailsControllerClass.java:187)
    at org.grails.web.mapping.mvc.UrlMappingsInfoHandlerAdapter.handle(UrlMappingsInfoHandlerAdapter.groovy:90)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)
    at org.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:77)
    at org.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:67)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.RuntimeException: org.grails.web.converters.exceptions.ConverterException: org.grails.web.json.JSONException: Value out of sequence: expected mode to be OBJECT or ARRAY when writing '{"a":"b"}' but was INIT
    at org.grails.web.converters.AbstractConverter.toString(AbstractConverter.java:111)
    at grails3.TestController$$EQ3IkyX7.index(TestController.groovy:25)
    ... 14 common frames omitted
Caused by: org.grails.web.converters.exceptions.ConverterException: org.grails.web.json.JSONException: Value out of sequence: expected mode to be OBJECT or ARRAY when writing '{"a":"b"}' but was INIT
    at grails.converters.JSON.value(JSON.java:193)
    at grails.converters.JSON.render(JSON.java:119)
    at org.grails.web.converters.AbstractConverter.toString(AbstractConverter.java:109)
    ... 15 common frames omitted
Caused by: org.grails.web.json.JSONException: Value out of sequence: expected mode to be OBJECT or ARRAY when writing '{"a":"b"}' but was INIT
    at org.grails.web.json.JSONWriter.append(JSONWriter.java:142)
    at org.grails.web.json.JSONWriter.value(JSONWriter.java:353)
    at grails.converters.JSON.value(JSON.java:162)
    ... 17 common frames omitted

Grails版本:3.2.3 Java版本:1.8u45和1.8u111

1 个答案:

答案 0 :(得分:0)

此语句本身将地图转换为JSON。

def val = [a: 101, b: '100'] as JSON

你不再需要这样做json = new JSON(json.toString())