简体   繁体   中英

JavaScript: Remove Double Quotes - JSON.Stringify()

After using he JSON.stringify() method in JavaScript to allow JSON data render in the browser it outputs double quotations " " at either end of the property rendered in the browser - any idea how to resolve this?

Here is my code -

<li class="divider">Brown Eyes</li>
<div id="output1"></div>
<li class="divider">Green Eyes</li>
<div id="output2"></div>

<script>
    var myContainer = "";

    var a = new XMLHttpRequest();
    a.open("GET", "https://api.myjson.com/bins/1dwnm", true);
    a.onreadystatechange = function () {
        console.log(a);
        if (a.readyState == 4) {
            var obj = JSON.parse(a.responseText);
            for (i = 0; i < obj.length; i++) {

                if (obj[i].eyeColor == 'brown') {
                var myContainer = "<ul class='list'><li>" + obj[i].name.first + " " + obj[i].name.last + " - " + obj[i].eyeColor + "</li></ul>";
                var myContainer = JSON.stringify(myContainer);
                document.getElementById('output1').innerHTML += myContainer;    
                }
                if (obj[i].eyeColor == 'green') {
                var myContainer = "<ul class='list'><li>" + obj[i].name.first + " " + obj[i].name.last + " - " + obj[i].eyeColor + "</li></ul>";
                var myContainer = JSON.stringify(myContainer);
                document.getElementById('output2').innerHTML += myContainer;    
                }
            }
        }

    }
    a.send();
</script>

Because " <ul class='list'><li>Faye Garrett - brown</li></ul> " is not a valid JSON string.

Uncomment your stringify lines and it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM