如何显示数组json字符串

时间:2017-05-30 20:39:15

标签: javascript html arrays json

我正在尝试显示json字符串中的数据。

我试过这个:



    <p id="demo"></p>

    <script>
        var thepds, x = "";

        thepds = {"end-point": "/api/sample/", "products": [{"is_customizable": "0", "delivery": "1-2 dage", "kids": "0", "name": "the name", "sizes": "37-39, 40-42, 43-45, 46-48", "kid_adult": "1", "free_porto": "0", "image": "image.png", "package": "0", "price": "119,00", "url": "theurl", "online": "1", "price_old": "119,00", "currency": "dkk", "img_url": "image.jpg", "id": "153339", "women": "0"}, {"is_customizable": "0", "delivery": "1-2 dage", "kids": "0", "name": "the name", "sizes": "37-39, 40-42, 43-45, 46-48", "kid_adult": "1", "free_porto": "0", "image": "image.png", "package": "0", "price": "119,00", "url": "theurl", "online": "1", "price_old": "119,00", "currency": "dkk", "img_url": "image.jpg", "id": "153339", "women": "0"}]};
        for (i in thepds.products) {
            x += "<h1>" + thepds.products[i].delivery + "</h1>";

        }
        document.getelementbyid("demo").innerhtml = x;

    </script>
&#13;
&#13;
&#13;

但它没有显示任何内容。

3 个答案:

答案 0 :(得分:0)

我认为问题是innerHTML属性的情况。正确的是:

document.getElementById("demo").innerHTML = x;

Javascript是一种区分大小写的语言。

答案 1 :(得分:0)

Javascript区分大小写。因此,getelementbyidinnerhtml应如下所示

&#13;
&#13;
    <p id="demo"></p>

    <script>
        var thepds, x = "";

        thepds = {"end-point": "/api/sample/", "products": [{"is_customizable": "0", "delivery": "1-2 dage", "kids": "0", "name": "the name", "sizes": "37-39, 40-42, 43-45, 46-48", "kid_adult": "1", "free_porto": "0", "image": "image.png", "package": "0", "price": "119,00", "url": "theurl", "online": "1", "price_old": "119,00", "currency": "dkk", "img_url": "image.jpg", "id": "153339", "women": "0"}, {"is_customizable": "0", "delivery": "1-2 dage", "kids": "0", "name": "the name", "sizes": "37-39, 40-42, 43-45, 46-48", "kid_adult": "1", "free_porto": "0", "image": "image.png", "package": "0", "price": "119,00", "url": "theurl", "online": "1", "price_old": "119,00", "currency": "dkk", "img_url": "image.jpg", "id": "153339", "women": "0"}]};
        for (i in thepds.products) {
            x += "<h1>" + thepds.products[i].delivery + "</h1>";

        }
        document.getElementById("demo").innerHTML = x;

    </script>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

即使我有印刷错误的麻烦。但我们在这里互相帮助。

这条线 document.getelementbyid(“demo”)。innerhtml = x;

应该是 document.getElementById(“demo”)。innerHTML = x;

不要气馁再问什么。并且不要试图阻止任何人提出要求。