1

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

Eric Frick
  • 847
  • 1
  • 7
  • 18
  • 1
    What's the use case? It's like asking about creating Frankenstein's monster... – Oded Feb 13 '12 at 16:54
  • Yes. What's the problem? – Brad Feb 13 '12 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 Feb 13 '12 at 16:55
  • 5
    You need to HTML-encode strings, as shown here: http://stackoverflow.com/questions/1219860/javascript-jquery-html-encoding – John Pick Feb 13 '12 at 16:57
  • It's possible, in jQuery you can use $(data.xml) to create a Dom object and parse it. But are you really sure that you need it in that way? – Flops Feb 13 '12 at 16:58
  • Thanks to jhon pick for the correct answer. – Eric Frick Feb 13 '12 at 17:05
  • 1
    for documentation purposes, could you please transform your comment into an answer @John_Pick sothat Eric can pick it as correct answer? – Gregor May 05 '17 at 10:29

2 Answers2

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;
Soumyadip Das
  • 1,781
  • 3
  • 16
  • 34
-2

Yes, of course. Your code is working well:

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