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

时间:2018-09-17 15:38:53

标签: java org.json

我的项目使用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]
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93) [spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_171]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_171]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_171]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [na:1.8.0_171]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_171]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_171]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_171]

我用于将XML转换为JSON的代码如下;

String fileContents = FileUtils.readFileToString(file, "UTF-8");
json = XML.toJSONObject(fileContents);

在我的POM文件中,我像这样定义依赖项;

<dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20180813</version>
        </dependency>

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

为什么我会看到此错误?

3 个答案:

答案 0 :(得分:0)

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

  <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>

答案 1 :(得分:0)

或者,您也可以将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();

答案 2 :(得分:0)

在我的情况下,这是因为依赖项“ spring-boot-starter-test”依赖于“ android-json”。排除后,它起作用了:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>com.vaadin.external.google</groupId>
            <artifactId>android-json</artifactId>
        </exclusion>
    </exclusions>
</dependency>

我正在使用20201115版本的json依赖项。