Java 使用JSON或JSON库从XML转换为JSON时出现问题

Java 使用JSON或JSON库从XML转换为JSON时出现问题,java,xml,json,json-lib,Java,Xml,Json,Json Lib,我在一些节点中使用包含一些html内容的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 on

我在一些节点中使用包含一些html内容的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>
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."}}
这是不恰当的


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

尝试从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));
import org.json.JSONObject;
导入org.json.XML;
字符串xmlString=学生应该避免在他们浏览的第一家书店购买课本。相反,他们应该调查其他在线书商提供的替代方案。在网上购物时,价格并不是唯一考虑的因素。学生在做出购买决定时还应考虑运费和交货时间。/P”;
System.out.println(“初始XML:+xmlString”);
JSONObject jsonObj=(XML.toJSONObject(xmlString));
System.out.println(“转换的JSON:+jsonObj.toString());
System.out.println(“返回到转换后的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>
最初的XML:学生应该避免在他们浏览的第一家商店购买教科书,而应该调查其他在线书商提供的替代品。价格不是在线购买时考虑的唯一因素。学生在制作B时也要考虑运输成本和交货时间。购买决策。/P 转换的JSON:{“详细信息”:学生应该避免在他们所浏览的第一家商店购买课本,而应该调查其他网上书商提供的替代品。价格不是在线购买时考虑的唯一因素。学生在购买决定时也要考虑运输成本和交货时间。 回到转换后的XML:学生应该避免在他们所浏览的第一家商店购买教科书。相反,他们应该调查其他在线书商提供的替代品。价格不是在线购买时考虑的唯一因素。g他们的购买决定。/P
你使用什么语言?PHP,java,JavaScript,C++,C语言,BASIC?你用什么来做转换?语言是java:<代码>导入Or.jSor.jSnObjor;;xml是包含xml内容的字符串。你应该用java标记你的问题,这样其他人就知道你在用什么语言,你把代码放在你的问题中,而不是放在评论中。
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>