javascript - Is it possible to write xml into json object? - Stack Overflow

admin2025-04-21  0

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.

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

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

Thanks in advance

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.

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

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

Thanks in advance

Share Improve this question asked Feb 13, 2012 at 16:52 Eric FrickEric Frick 8572 gold badges7 silver badges18 bronze badges 7
  • 1 What's the use case? It's like asking about creating Frankenstein's monster... – Oded Commented Feb 13, 2012 at 16:54
  • Yes. What's the problem? – Brad Commented Feb 13, 2012 at 16:54
  • THe problem is that the web service isn't receiving the get request when sending a object like this, so I think it's a parse error – Eric Frick Commented Feb 13, 2012 at 16:55
  • 5 You need to HTML-encode strings, as shown here: stackoverflow./questions/1219860/… – John Pick Commented Feb 13, 2012 at 16:57
  • 1 for documentation purposes, could you please transform your ment into an answer @John_Pick sothat Eric can pick it as correct answer? – Gregor Commented May 5, 2017 at 10:29
 |  Show 2 more ments

2 Answers 2

Reset to default 2

Your XML will be like this:

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

Replace as follows:

& = &amp;

" = &quot;

' = &#39;

< = &lt;

> = &gt;

/ = &#x2F;

Yes, of course. Your code is working well:

var obj = {"a":"a","b":"b", "xml":"<test r='r'></test>"}
alert(obj.xml)
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745172541a288745.html

最新回复(0)