-1

I have a list that i am trying to parse to a json object, ["Harry ", "Potter ", "Name ", "Batman"]. I am using Django templates for this. I have parsed the list using simplejson.dumps(). I am trying to parse this in my javascript code.It is working fine until the line document.getElementById("demo").innerHTML = x; in my code, it is printing ["Harry ", "Potter ", "Name ", "Batman"] correctly.but JSON.parse() is not working.The lines written after calling parse are not working. Can anyone tell me the problem with this code?

{%extends 'base.html'%}

{%block content%}

<p id="demo">hai {{y}} im</p>

<script>

var x = (("{{y}}").replace(/&(l|g|quo)t;/g, function(a,b){
               return {
                   l   : '<',
                   g   : '>',
                   quo : '"'
               }[b];
           }));
x = x.replace(/u'/g, '\'');
x = x.replace(/'/g, '\"');
document.getElementById("demo").innerHTML = x;
p = JSON.parse( x );
document.getElementById("demo").innerHTML="Aray";
</script>
{%endblock%}

Thanks

Xyz
  • 5,955
  • 5
  • 40
  • 58
Unknown
  • 67
  • 7

1 Answers1

0

Maybe you need to stringify it before you parse it. Look at the example below:

var arr = ["a", "b", "c"];
var str = JSON.stringify(arr);
document.write(str);
document.write ("<br/>");

var newArr = JSON.parse(str);
jcs
  • 426
  • 3
  • 14