2

我正在尝试使用 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

4

1 回答 1

0

该语句本身将映射转换为 JSON。

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

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

于 2016-11-22T17:20:04.253 回答