使用json库进行json转换

时间:2016-02-21 06:31:27

标签: java json xml converter

我正在尝试使用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"
      }
    ]
  }
}

1 个答案:

答案 0 :(得分:1)

我建议您使用XSLT将源XML转换为XML版本,以便更容易地将其转换为您正在寻找的JSON。

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