从XML动态获取值并将其放入JSON数组,然后放入String Array中

时间:2016-02-15 20:18:21

标签: java arrays json xml

我正在尝试将以下XML转换为JSON数组,然后转换为String数组:

<property name="productIcon">
  <jsonproperty key="productImage"><![CDATA[AAAAA - Lorem Ipsum is simply dummy text of the printing and typesetting industry.]]></jsonproperty>
  <jsonproperty key="produtIconName"><![CDATA[BBBBB - Lorem Ipsum is simply dummy text of the printing and typesetting industry.]]></jsonproperty>
  <jsonproperty key="produtIconDesc"><![CDATA[CCCCC - Lorem Ipsum is simply dummy text of the printing and typesetting industry]]></jsonproperty>

  <jsonproperty key="productImage"><![CDATA[11111 - Lorem Ipsum is simply dummy text of the printing and typesetting industry.]]></jsonproperty>
  <jsonproperty key="produtIconName"><![CDATA[22222 - Lorem Ipsum is simply dummy text of the printing and typesetting industry.]]></jsonproperty>
  <jsonproperty key="produtIconDesc"><![CDATA[33333 - Lorem Ipsum is simply dummy text of the printing and typesetting industry]]></jsonproperty>
</property>

这是我试图实现的最终输出:

[
{"productImage":" AAAAA - Lorem Ipsum is simply dummy text of the printing and typesetting industry ",
"produtIconName":" BBBBB - Lorem Ipsum is simply dummy text of the printing and typesetting industry ",
"produtIconDesc":" CCCCC - Lorem Ipsum is simply dummy text of the printing and typesetting industry "}”,
{"productImage":" 11111 - Lorem Ipsum is simply dummy text of the printing and typesetting industry ",
"produtIconName":" 22222 - Lorem Ipsum is simply dummy text of the printing and typesetting industry ",
"produtIconDesc":" 33333 - Lorem Ipsum is simply dummy text of the printing and typesetting industry "}”
}]

这就是我的尝试:

if(node.getNodeName().equals("key")) {
    JSONObject[] test = new JSONObject();
    test.push(node.getNodeValue(),tempNode.getTextContent());
    System.out.println("JSON : " + jsonArray);
}

但这不起作用。我如何达到预期的效果?

0 个答案:

没有答案