首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用JSON或json-lib将XML转换为json的问题

使用JSON或json-lib将XML转换为json的问题
EN

Stack Overflow用户
提问于 2013-08-08 05:48:39
回答 1查看 892关注 0票数 1

在某些节点中包含一些html内容的xml如下

代码语言:javascript
复制
<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,

代码语言:javascript
复制
import net.sf.json.JSON;
import net.sf.json.xml.XMLSerializer; 
.....
JSONObject jsonObject = XML.toJSONObject(xml.toString());
......

xml是包含xml内容的字符串。

我应该得到json字符串

代码语言:javascript
复制
{"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;"}

但是输出就像

代码语言:javascript
复制
{"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."}}

这是不恰当的。

有人能告诉我如何解决这个问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-05 08:03:16

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

代码语言:javascript
复制
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));

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

代码语言:javascript
复制
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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18118748

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档