gpt4 book ai didi

java - 使用 json 或 json-lib 从 XML 转换为 JSON 时出现问题

转载 作者:行者123 更新时间:2023-12-01 04:29:33 24 4
gpt4 key购买 nike

我的 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."}}

这是不正确的。

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

最佳答案

尝试使用 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>

关于java - 使用 json 或 json-lib 从 XML 转换为 JSON 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18118748/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com