Qyouu
将以下依赖项添加到您的应用程序:<dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20180813</version></dependency>它提供了一种非常简单的方式将 XML 文档转换为 JSON 文档:String xml = "<header>\n" + " <students>\n" + " <info>\n" + " <name>student1</name>\n" + " <class>2</class>\n" + " </info>\n" + " <info>\n" + " <name>student2</name>\n" + " <class>3</class>\n" + " </info>\n" + " </students>\n" + "</header>";JSONObject json = XML.toJSONObject(xml);System.out.println(json);输出将是:{"header":{"students":{"info":[{"name":"student1","class":2},{"name":"student2","class":3}]}}}然后你可以执行一些操作:JSONArray info = json.getJSONObject("header").getJSONObject("students").getJSONArray("info");json.getJSONObject("header").put("students", info);System.out.println(json);您将获得所需的输出:{"header":{"students":[{"name":"student1","class":2},{"name":"student2","class":3}]}}