java - 将 xml 转换为 json 不会以相同的形式打印 xml
问题描述
我正在尝试将 xml 转换为 json,我想使用 1:1
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>
但问题是转换后的输出形式不一样。代码:
JSONObject xmlJSONObj = XML.toJSONObject(TEST_XML_STRING);
String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);
System.out.println(jsonPrettyPrintString);
xml:
<?xml version="1.0" encoding="UTF-8"?>
<!--Anagrafica del clienti del mercato-->
<anagrafica>
<testata>
<nomemercato id="007">Mercato di test</nomemercato>
<data>Giovedi 18 dicembre 2003 16.05.29</data>
</testata>
<record>
<codice_cliente>5</codice_cliente>
<rag_soc>Miami American Cafe</rag_soc>
<codice_fiscale>IT07654930130</codice_fiscale>
<indirizzo tipo="casa">Viale Carlo Espinasse 5, Como</indirizzo>
<num_prodotti>13</num_prodotti>
</record>
...
</anagrafica>
我得到了代码
{"anagrafica": {
"record": [
{
"codice_fiscale": "IT07654930130",
"rag_soc": "Miami American Cafe",
"num_prodotti": 13,
"codice_cliente": 5,
"indirizzo": {
"tipo": "casa",
"content": "Viale Carlo Espinasse 5, Como"
}
....
],
"testata": {
"data": "Giovedi 18 dicembre 2003 16.05.29",
"nomemercato": {
"id": "007",
"content": "Mercato di test"
}
}
}}
正如你所看到testata
的,record
我的意思是 testata 应该是第一个,然后是记录。你能告诉我如何解决这个问题吗?或者我应该用什么来使它正确?
解决方案
推荐阅读
- python - 数学运算符或 tf.function - 应该使用哪一个?
- spring - 当应用程序位于 SSL 终止代理之后时,如何配置 Spring Boot 以使用 OIDC
- python - 如何计算熊猫数据框中列值更改的频率?
- mysql - 带有mysql rest api的节点js GETTING错误:getaddrinfo ENOENT localhost
- javascript - 如何将 counterup2 转换为 vanilla javascript
- dolphindb - 在 DolphinDB 中如何将表中的行转换为向量
- python - 如何使用别名/查找表来引用数据框列
- wkwebview - 在 iOS 上的现有生产应用程序上调试 wkwebview
- javascript - 在使用 Google Publisher Tag 时了解 Google 广告管理器提供的广告类型的方法
- python - 为什么 .remove inside for 循环不从 Python 列表中删除所有实例?