gpt4 book ai didi

java - 使用json库进行json转换

转载 作者:行者123 更新时间:2023-12-01 10:22:02 25 4
gpt4 key购买 nike

我正在尝试使用 json 库将 xml 转换为特定格式的 json。 xml 已转换为 json,但我不知道如何从中获取自定义值。

谁能告诉我一些解决方案

我的代码如下所示

转换代码

File file = new File ("D:\\data.xml");
inputStream = new FileInputStream(file);
StringBuilder builder = new StringBuilder();
int ptr = 0;
while ((ptr = inputStream.read()) != -1 )
{
builder.append((char) ptr);
}

String xml = builder.toString();
JSONObject jsonObj = XML.toJSONObject(xml);
System.out.println(jsonObj);

data.xml

<?xml version="1.0"?>
<root>
<data-element name="Parent1" type="parent" index="0">
<data-element name="Pages" index="0" type="text">
<data-element name="Base" index="0" type="text">
<data-element name="Page" index="0" type="text">
<data-element name="Value" index="0" type="text">
<data-content>
<![CDATA[Apple]]>
</data-content>
</data-element>
<data-content>
<![CDATA[Red Color]]>
</data-content>
</data-element>
<data-element name="Page" index="1" type="text">
<data-element name="Value" index="1" type="text">
<data-content>
<![CDATA[Orange]]>
</data-content>
</data-element>
<data-content>
<![CDATA[Orange Color]]>
</data-content>
</data-element>
<data-content>
<![CDATA[Fruits]]>
</data-content>
</data-element>
<data-element name="Base" index="0" type="text">
<data-element name="Page" index="0" type="text">
<data-element name="Value" index="0" type="text">
<data-content>
<![CDATA[Violet]]>
</data-content>
</data-element>
<data-content>
<![CDATA[Beetroot]]>
</data-content>
</data-element>
<data-element name="Page" index="1" type="text">
<data-element name="Value" index="1" type="text">
<data-content>
<![CDATA[Orange]]>
</data-content>
</data-element>
<data-content>
<![CDATA[Carrot]]>
</data-content>
</data-element>
<data-content>
<![CDATA[Vegetables]]>
</data-content>
</data-element>
<data-content type="Child">
<![CDATA[Category1]]>
</data-content>
</data-element>
</data-element>
<data-element name="Parent2" type="parent" index="0">
<data-element name="Pages" index="0" type="text">
<data-element name="Base" index="0" type="text">
<data-element name="Page" index="0" type="text">
<data-element name="Value" index="0" type="text">
<data-content>
<![CDATA[Apple]]>
</data-content>
</data-element>
<data-content>
<![CDATA[Red Color]]>
</data-content>
</data-element>
<data-element name="Page" index="1" type="text">
<data-element name="Value" index="1" type="text">
<data-content>
<![CDATA[Central Processing Unit]]>
</data-content>
</data-element>
<data-content>
<![CDATA[CPU]]>
</data-content>
</data-element>
<data-content>
<![CDATA[Computer]]>
</data-content>
</data-element>
<data-element name="Base" index="0" type="text">
<data-element name="Page" index="0" type="text">
<data-element name="Value" index="0" type="text">
<data-content>
<![CDATA[Mobile]]>
</data-content>
</data-element>
<data-content>
<![CDATA[Android]]>
</data-content>
</data-element>
<data-element name="Page" index="1" type="text">
<data-element name="Value" index="1" type="text">
<data-content>
<![CDATA[Java]]>
</data-content>
</data-element>
<data-content>
<![CDATA[Open Source]]>
</data-content>
</data-element>
<data-content>
<![CDATA[Programming]]>
</data-content>
</data-element>
<data-content type="Child">
<![CDATA[Category2]]>
</data-content>
</data-element>
</data-element>
</root>

预期 Json

{
"Parent1": {
"Category1": {
"Fruits": {
"Red Color": "Apple",
"Orange Color": "Orange"
},
"Vegetables": {
"Beetroot": "Violet",
"Carrot": "Orange"
}
}
},
"Parent2": {
"Category2": {
"Computer": {
"Android": "Mobile"
},
"Programming": {
"Open Source": "Java"
}
}
}
}

转换后的 json 如下所示

{
"root": {
"data-element": [
{
"index": 0,
"name": "Parent1",
"data-element": {
"index": 0,
"name": "Pages",
"data-content": {
"content": "Category1",
"type": "Child"
},
"data-element": [
{
"index": 0,
"name": "Base",
"data-content": "Fruits",
"data-element": [
{
"index": 0,
"name": "Page",
"data-content": "Red Color",
"data-element": {
"index": 0,
"name": "Value",
"data-content": "Apple",
"type": "text"
},
"type": "text"
},
{
"index": 1,
"name": "Page",
"data-content": "Orange Color",
"data-element": {
"index": 1,
"name": "Value",
"data-content": "Orange",
"type": "text"
},
"type": "text"
}
],
"type": "text"
},
{
"index": 0,
"name": "Base",
"data-content": "Vegetables",
"data-element": [
{
"index": 0,
"name": "Page",
"data-content": "Beetroot",
"data-element": {
"index": 0,
"name": "Value",
"data-content": "Violet",
"type": "text"
},
"type": "text"
},
{
"index": 1,
"name": "Page",
"data-content": "Carrot",
"data-element": {
"index": 1,
"name": "Value",
"data-content": "Orange",
"type": "text"
},
"type": "text"
}
],
"type": "text"
}
],
"type": "text"
},
"type": "parent"
},
{
"index": 0,
"name": "Parent2",
"data-element": {
"index": 0,
"name": "Pages",
"data-content": {
"content": "Category2",
"type": "Child"
},
"data-element": [
{
"index": 0,
"name": "Base",
"data-content": "Computer",
"data-element": [
{
"index": 0,
"name": "Page",
"data-content": "Red Color",
"data-element": {
"index": 0,
"name": "Value",
"data-content": "Apple",
"type": "text"
},
"type": "text"
},
{
"index": 1,
"name": "Page",
"data-content": "CPU",
"data-element": {
"index": 1,
"name": "Value",
"data-content": "Central Processing Unit",
"type": "text"
},
"type": "text"
}
],
"type": "text"
},
{
"index": 0,
"name": "Base",
"data-content": "Programming",
"data-element": [
{
"index": 0,
"name": "Page",
"data-content": "Android",
"data-element": {
"index": 0,
"name": "Value",
"data-content": "Mobile",
"type": "text"
},
"type": "text"
},
{
"index": 1,
"name": "Page",
"data-content": "Open Source",
"data-element": {
"index": 1,
"name": "Value",
"data-content": "Java",
"type": "text"
},
"type": "text"
}
],
"type": "text"
}
],
"type": "text"
},
"type": "parent"
}
]
}
}

最佳答案

我建议您考虑使用 XSLT 将源 XML 转换为 XML 版本,以便更轻松地转换为您正在寻找的 JSON。

或者,您可能需要使用 SAX 解析器来迭代 XML 并以编程方式创建 JSON。

关于java - 使用json库进行json转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35533064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com