【问题标题】:Issue with conversion from XML to JSON with json or json-lib使用 json 或 json-lib 从 XML 转换为 JSON 的问题
【发布时间】:2014-11-05 16:03:16
【问题描述】:

我的 xml 在某些节点中包含一些 html 内容,例如

<detail>
&lt;P&gt;Students should avoid purchasing their textbooks at the first store that they browse. Instead, they should investigate the alternatives offered by other online booksellers. Price isn't the only factor to consider when making an online purchase. Students should also factor in shipping costs and delivery time when making their buying decision.&lt;/P&gt;
</detail>

通过 json 或 json-lib 将 xml 转换为 Json 时,通过

import net.sf.json.JSON;
import net.sf.json.xml.XMLSerializer; 
.....
JSONObject jsonObject = XML.toJSONObject(xml.toString());
......

xml 是包含 xml 内容的字符串

我应该得到 json 字符串为

{"detail":"&lt;P&gt;Students should avoid purchasing their textbooks at the first store that they browse. Instead, they should investigate the alternatives offered by other online booksellers. Price isn't the only factor to consider when making an online purchase. Students should also factor in shipping costs and delivery time when making their buying decision.&lt;/P&gt;"}

但输出类似于

{"detail":{"P":"Students should avoid purchasing their textbooks at the first store that they browse. Instead, they should investigate the alternatives offered by other online booksellers. Price isn't the only factor to consider when making an online purchase. Students should also factor in shipping costs and delivery time when making their buying decision."}}

这是不对的。

谁能告诉我如何解决这个问题?

【问题讨论】:

  • 您使用什么语言? php、java、javascript、C++、C#、BASIC?您用来进行转换的代码在哪里?
  • 语言是 java:import org.json.JSONObject; import org.json.XML;JSONObject jsonObject = XML.toJSONObject(xml); xml 是包含 xml 内容的字符串。
  • 您应该用 java 标记您的问题,而不是让其他人知道您使用的是什么语言,并且您将代码放在问题中而不是 cmets。

标签: java xml json json-lib


【解决方案1】:

尝试使用来自 www.json.org 的库而不是 net.sf.json.JSON

import org.json.JSONObject;
import org.json.XML;

String xmlString = "<detail>&lt;P&gt;Students should avoid purchasing their textbooks at the first store that they browse. Instead, they should investigate the alternatives offered by other online booksellers. Price isn't the only factor to consider when making an online purchase. Students should also factor in shipping costs and delivery time when making their buying decision.&lt;/P&gt;</detail>";
System.out.println("Initial XML : " + xmlString);
JSONObject jsonObj = (XML.toJSONObject(xmlString));
System.out.println("Converted JSON : " + jsonObj.toString());
System.out.println("Back to converted XML : " + XML.toString(jsonObj));

你会得到这个在我看来更好的结果:

Initial XML : <detail>&lt;P&gt;Students should avoid purchasing their textbooks at the first store that they browse. Instead, they should investigate the alternatives offered by other online booksellers. Price isn't the only factor to consider when making an online purchase. Students should also factor in shipping costs and delivery time when making their buying decision.&lt;/P&gt;</detail>
Converted JSON : {"detail":"<P>Students should avoid purchasing their textbooks at the first store that they browse. Instead, they should investigate the alternatives offered by other online booksellers. Price isn't the only factor to consider when making an online purchase. Students should also factor in shipping costs and delivery time when making their buying decision.<\/P>"}
Back to converted XML : <detail>&lt;P&gt;Students should avoid purchasing their textbooks at the first store that they browse. Instead, they should investigate the alternatives offered by other online booksellers. Price isn&apos;t the only factor to consider when making an online purchase. Students should also factor in shipping costs and delivery time when making their buying decision.&lt;/P&gt;</detail>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 2014-11-26
    • 2014-03-27
    • 2018-08-30
    • 1970-01-01
    相关资源
    最近更新 更多