javascript 是否可以将xml写入json对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9264470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:07:09  来源:igfitidea点击:

Is it possible to write xml into json object?

javascriptjqueryxmljson

提问by Eric Frick

I'm having issues in javascript with putting a var that contains xml string into a object and then send it via ajax with jQuery.

我在 javascript 中遇到问题,将包含 xml 字符串的 var 放入对象中,然后使用 jQuery 通过 ajax 发送它。

The question would be: is it possible to have a xml string into a json object ?

问题是:是否可以将 xml 字符串放入 json 对象中?

{"a":"a","b":"b", "xml":"<test r='r'></test>"}

Thanks in advance

提前致谢

回答by Soumyadip Das

Your XML will be like this:

您的 XML 将是这样的:

&lt; test r=&quot;r&quot;&gt; &lt;&#x2F;test&gt;

Replace as follows:

替换如下:

& = &amp;

" = &quot;

' = &#39;

< = &lt;

> = &gt;

/ = &#x2F;

回答by Ivan Kolodyazhny

Yes, of course. Your code is working well:

当然是。您的代码运行良好:

var obj = {"a":"a","b":"b", "xml":"<test r='r'></test>"}
alert(obj.xml)