将 xml 映射到 Json

我想将 xml 转换为 JSON。示例:将以下 xml 转换为给定的 json。


<header>

<students>

<info>

    <name>student1</name>

    <class>2</class>

</info>

<info>

<name>student2</name>

<class>3</class>

</info>

</students>

</header>

JSON:


{

"header": {

    "students": [

        {

            "name": "student1",

            "class": "2"

        },

        {

            "name": "student2",

            "class": "3"

        }

    ]

}

}

问题是,如何将元素转换为数组?


潇潇雨雨
浏览 179回答 3
3回答

Qyouu

将以下依赖项添加到您的应用程序:<dependency>&nbsp; &nbsp; <groupId>org.json</groupId>&nbsp; &nbsp; <artifactId>json</artifactId>&nbsp; &nbsp; <version>20180813</version></dependency>它提供了一种非常简单的方式将 XML 文档转换为 JSON 文档:String xml = "<header>\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"&nbsp; <students>\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"&nbsp; &nbsp; <info>\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"&nbsp; &nbsp; &nbsp; <name>student1</name>\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"&nbsp; &nbsp; &nbsp; <class>2</class>\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"&nbsp; &nbsp; </info>\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"&nbsp; &nbsp; <info>\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"&nbsp; &nbsp; &nbsp; <name>student2</name>\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"&nbsp; &nbsp; &nbsp; <class>3</class>\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"&nbsp; &nbsp; </info>\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"&nbsp; &nbsp;</students>\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"</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}]}}

侃侃无极

我试图从您添加的 xml 字符串创建一个 jason。使用 c# 并能够获取 Jason 文件。您应该将 nuget 包 Newtonsoft.Json 添加到您的项目中。&nbsp;public void xmlToJason()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; string xml = @"<header>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <students>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <info>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <name>student1</name>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <class>2</class>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </info>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <info>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <name>student2</name>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <class>3</class>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </info>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </students>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </header>";&nbsp; &nbsp; &nbsp; &nbsp; XmlDocument doc = new XmlDocument();&nbsp; &nbsp; &nbsp; &nbsp; doc.LoadXml(xml);&nbsp; &nbsp; &nbsp; &nbsp; string json = JsonConvert.SerializeXmlNode(doc);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(json);&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java