猿问

使用 org.json 将 XML 转换为 JSON 时出现 NoSuchMethod 错误

我的项目使用 Spring Boot 2.0.4。我正在尝试从文件中读取 XML,然后将其转换为 JSON。这曾经有效,但最近它停止工作并抛出以下异常;


java.lang.NoSuchMethodError: org.json.JSONTokener.<init>(Ljava/io/Reader;)V

    at org.json.XMLTokener.<init>(XMLTokener.java:57) ~[json-20180813.jar:na]

    at org.json.XML.toJSONObject(XML.java:516) ~[json-20180813.jar:na]

    at org.json.XML.toJSONObject(XML.java:548) ~[json-20180813.jar:na]

    at org.json.XML.toJSONObject(XML.java:472) ~[json-20180813.jar:na]

    at com.zf.trw.visualisation.parser.handler.AttritionHandler.extractLineData(AttritionHandler.java:32) ~[classes/:na]

    at com.zf.trw.visualisation.parser.handler.HandlerImp.processFile(HandlerImp.java:79) ~[classes/:na]

    at com.zf.trw.visualisation.shared.service.ParserService.manuallyProcessAttritionData(ParserService.java:85) ~[classes/:na]

    at com.zf.trw.visualisation.parser.component.ScheduledTask.processAttritionDataFilesForAllLines(ScheduledTask.java:49) ~[classes/:na]

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_171]

    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_171]

    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_171]

    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_171]

    at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]

异常似乎表明它正在尝试使用我在 POM 文件中指定的相同版本,这是最新版本。


为什么我看到这个错误?


喵喔喔
浏览 787回答 3
3回答

Cats萌萌

为我们做以下工作,在 pom.xml 中,我们排除了这两个定义它们自己的类 org.json.JSONObject 的依赖项: com.unboundid.components:json和com.unboundid.components:mimepull。IE:&nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-web-services</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; <exclusions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <exclusion>&nbsp; <!-- declare the exclusion here -->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.jvnet.mimepull</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>mimepull</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </exclusion>&nbsp; &nbsp; &nbsp; &nbsp; </exclusions>&nbsp; &nbsp; &nbsp; &nbsp; <version>${spring.boot.version}</version>&nbsp; &nbsp;</dependency>

MM们

或者,您也可以将 XML 转换为 JSON,然后可以根据 JSON 模式验证 JSON。依赖:<dependency>&nbsp; &nbsp; <groupId>com.github.fge</groupId>&nbsp; &nbsp; <artifactId>json-schema-validator</artifactId>&nbsp; &nbsp; <version>2.2.6</version></dependency>代码:JsonNode node = JsonLoader.fromString(builder.toString());JsonNode data = JsonLoader.fromString(serviceCreateJson.trim());JsonSchemaFactory factory = JsonSchemaFactory.byDefault();JsonSchema sch = factory.getJsonSchema(node);ProcessingReport report = sch.validate(data);boolean isSuccess = report.isSuccess();
随时随地看视频慕课网APP

相关分类

Java
我要回答