我在某个节点中有一些包含某些html内容的xml,比如
<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>
</detail>
通过json或json-lib从
import net.sf.json.JSON;
import net.sf.json.xml.XMLSerializer;
.....
JSONObject jsonObject = XML.toJSONObject(xml.toString());
......
xml是包含xml内容的字符串
我应该把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>"}
但输出类似于
{"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."}}
这是不恰当的。
任何人都可以告诉我该如何解决这个问题?
答案 0 :(得分:0)
尝试使用www.json.org上的库而不是net.sf.json.JSON
import org.json.JSONObject;
import org.json.XML;
String xmlString = "<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></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><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></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><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></detail>