【问题标题】:Not well formatted -XML to Json Conversion格式不正确 -XML 到 Json 的转换
【发布时间】:2026-01-29 18:30:01
【问题描述】:
<list>
<interfaceReachability  type="Critical" count="345" httpLink="searchLradIfAction_pageId?isAscending=true&operation=monitor&searchApByType=AllTypes&searchRadioTypeallRadios=true&searchApType=homePage&severity=1" />
<interfaceReachability  type="No Alarms" count="6216" httpLink="searchLradIfAction_pageId?isAscending=true&operation=monitor&searchApByType=AllTypes&searchRadioTypeallRadios=true&searchApType=homePage&severity=5" />
<interfaceReachability  type="Minor" count="145" httpLink="searchLradIfAction_pageId?isAscending=true&operation=monitor&searchApByType=AllTypes&searchRadioTypeallRadios=true&searchApType=homePage&severity=3" />
</list>

我想将此 XML 转换为 Json 。由于此 XML 格式不正确且元素包含“&”和“?” Org.json 没有转换 this 。任何帮助将非常感激 。我真的卡在这里了。我试过了

org.json.JSONObject xmlJSONObj = XML.toJSONObject(TEST_XML_STRING);
                 String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);
                System.out.println(jsonPrettyPrintString);

但没有运气。再次感谢

【问题讨论】:

  • 哪个xml?多一点信息真的很有帮助。
  • 你试过用Xstream吗?我认为您可以将 xml 读取为对象,然后使用 json writer:xstream.codehaus.org/json-tutorial.html。示例 xml 肯定会有所帮助。
  • @Thomas 我现在添加了 xml。@Petro,如果您不介意,请添加代码。我也尝试了 Xstream,但我无法。顺便说一句,xml 标记的名称将动态更改。感谢您的帮助

标签: java xml json


【解决方案1】:

您必须将出现的 '&amp;' 替换为 &amp;amp; 实体,否则它不是有效的 XML。

【讨论】: