我使用URL访问REST API,它给出了以下JSON结果:
{"size":1,"filter":{"applicationName":"xx.x1",
"fromTimestamp":1261746800000,
"toTimestamp":1361833200000,
"company":"xx",
"groupedBy":"COMPANY_APPLICATION"},
"values"[{"applicationName":"xx.x1","count":17,"company":"xx"}],
"start":0,"limit":25,"lastPage":true}
使用Jackson我尝试以这种方式解析json:
ObjectMapper mapper = new ObjectMapper();
try {
ErrorDataRest error = mapper.readValue(url, ErrorDataRest.class);
Map<String,Object> map = mapper.readValue(url, Map.class);
无论我使用的是第一个还是第二个,我都会遇到这个例外:
dk.jyskebank.jee.core.exception.JyskeSystemException: Auto generated exception
at dk.sd.dumpmonitor.domainservice.RestErrorServiceBean.queryRESTurl(RestErrorServiceBean.java:46)
at service.RestErrorServiceTest.testQueryRESTurl(RestErrorServiceTest.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.codehaus.jackson.JsonParseException: Unexpected character ('h' (code 104)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
at [Source: java.io.StringReader@30d82d; line: 1, column: 2]
at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1433)
at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:521)
at org.codehaus.jackson.impl.JsonParserMinimalBase._reportUnexpectedChar(JsonParserMinimalBase.java:442)
at org.codehaus.jackson.impl.ReaderBasedParser._handleUnexpectedValue(ReaderBasedParser.java:1198)
at org.codehaus.jackson.impl.ReaderBasedParser.nextToken(ReaderBasedParser.java:485)
at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2770)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2718)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1863)
at dk.sd.dumpmonitor.domainservice.RestErrorServiceBean.queryRESTurl(RestErrorServiceBean.java:43)
... 25 more
我不知道有什么问题或者如何解决它。
我无法访问其余的API,但我知道它使用RESTeasy。
我唯一想要的是将JSON响应映射到Java类,所以也许还有其他方法可以做到这一点?
答案 0 :(得分:26)
我猜你的变量url
是String
,而不是URL
,所以杰克逊正在解析网址,好像它是一个真正的JSON字符串。这可以解释“意外字符'h'”,因为这可能是你的URL的第一个字母(即http ......某些东西。)代码“104”对应于“h”的ASCII值。
如果您使用mapper.readValue(new URL(url), ErrorDataRest.class);
它是否有效?
答案 1 :(得分:0)
在我(笨拙地)使用echo
将XML导入测试文件后,发生了这种情况:
echo "<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope"><s:Envelope>" \
> sampleXml
当然,它丢失了所有引号,并为我造成了这个问题。
确保所有属性都有引号!