为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

喵喔喔 2021-09-26 17:02:29
我的项目使用 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 文件中指定的相同版本,这是最新版本。为什么我看到这个错误?
查看完整描述

3 回答

?
Cats萌萌

TA贡献1805条经验 获得超9个赞

为我们做以下工作,在 pom.xml 中,我们排除了这两个定义它们自己的类 org.json.JSONObject 的依赖项: com.unboundid.components:json和com.unboundid.components:mimepull。IE:


  <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-web-services</artifactId>

        <exclusions>

            <exclusion>  <!-- declare the exclusion here -->

                <groupId>org.jvnet.mimepull</groupId>

                <artifactId>mimepull</artifactId>

            </exclusion>

        </exclusions>

        <version>${spring.boot.version}</version>

   </dependency>


查看完整回答
反对 回复 2021-09-26
?
MM们

TA贡献1886条经验 获得超2个赞

或者,您也可以将 XML 转换为 JSON,然后可以根据 JSON 模式验证 JSON。


依赖:


<dependency>

    <groupId>com.github.fge</groupId>

    <artifactId>json-schema-validator</artifactId>

    <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();


查看完整回答
反对 回复 2021-09-26
  • 3 回答
  • 0 关注
  • 786 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信