I have the following JSON structure :
我有以下JSON结构:
{
"userId": "55",
"Unit": [
{
"id": "1",
"unitname": "unit1",
"eventId": "2",
"transactiontype": "1"
},
{
"id": "2",
"unitname": "unit2",
"eventId": "2",
"transactiontype": "1"
},
{
"id": "3",
"unitname": "unit3",
"eventId": "2",
"transactiontype": "2"
}
]
}
and I need to convert it to the below XMl format :
我需要把它转换成下面的XMl格式:
<Units userId="55">
<Unit id="1" unitname="unit1" eventId="2" transactiontype="1"/>
<Unit id="2" unitname="unit2" eventId="2" transactiontype="1"/>
<Unit id="3" unitname="unit3" eventId="2" transactiontype="2"/>
</Units>
While trying it through java I ma getting an XML but it shows XML elements as below :
通过java我得到了一个XML,但是它显示了如下的XML元素:
<UnitId>1</UnitId>
Can someone please help me as to what needs to be done so that I get the XML format needed, i.e as attributes.
谁能帮助我做什么,以便我得到需要的XML格式,我。e作为属性。
2 个解决方案
#1
4
Maybe you can use the json.org library. I'm not sure if this library does exactly what you want.
也许你可以使用json.org库。我不确定这个库是否完全符合您的要求。
You can use it like that:
你可以这样使用:
JSONObject json = new JSONObject(str);
String xml = XML.toString(json);
toString
can take a second argument to provide the name of the XML root node.
toString可以使用第二个参数来提供XML根节点的名称。
XML to JSON using XML.toJSONObject(java.lang.String)
使用XML. tojsonobject (java.lang.String)从XML到JSON
POM
砰的一声
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20170516</version>
</dependency>
#2
0
Here is an XSLT 3.0 solution.
这里是一个XSLT 3.0解决方案。
<xsl:transform version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="main">
<xsl:variable name="in" select="json-doc('input.json')" as="map(*)"/>
<Units userId="{$in?userId}">
<xsl:for-each select="$in?Unit?*"
<Unit id="{?id}" unitname="{?unitname}"
eventId="{?eventId}" transactiontype="{?transactiontype}"/>
</xsl:for-each>
</Units>
</xsl:template>
</xsl:transform>
You can run this from Java by installing Saxon-PE 9.7.
您可以通过安装Saxon-PE 9.7从Java运行它。
#1
4
Maybe you can use the json.org library. I'm not sure if this library does exactly what you want.
也许你可以使用json.org库。我不确定这个库是否完全符合您的要求。
You can use it like that:
你可以这样使用:
JSONObject json = new JSONObject(str);
String xml = XML.toString(json);
toString
can take a second argument to provide the name of the XML root node.
toString可以使用第二个参数来提供XML根节点的名称。
XML to JSON using XML.toJSONObject(java.lang.String)
使用XML. tojsonobject (java.lang.String)从XML到JSON
POM
砰的一声
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20170516</version>
</dependency>
#2
0
Here is an XSLT 3.0 solution.
这里是一个XSLT 3.0解决方案。
<xsl:transform version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="main">
<xsl:variable name="in" select="json-doc('input.json')" as="map(*)"/>
<Units userId="{$in?userId}">
<xsl:for-each select="$in?Unit?*"
<Unit id="{?id}" unitname="{?unitname}"
eventId="{?eventId}" transactiontype="{?transactiontype}"/>
</xsl:for-each>
</Units>
</xsl:template>
</xsl:transform>
You can run this from Java by installing Saxon-PE 9.7.
您可以通过安装Saxon-PE 9.7从Java运行它。