javascript - 使用 jQuery 将 JSON 中的元素转换为 html 不起作用

标签 javascript json

我正在使用以下代码从该 JSON 中获取一些元素,但似乎没有任何效果。有人知道我做错了什么吗? (我花了很长时间试图弄清楚)。

$(document).ready(function() {

$.getJSON('http://free.worldweatheronline.com/feed/weather.ashx?q=Stockholm&format=json&num_of_days=2&key=e8536d3a52101433121710', function(Wdata) {
                $.each(Wdata.data, function() {

                $('<div id="test"></div>').append(

                        this.weather[0].date;

                        ).appendTo('body');

                  });
    });
});

我的 HTML 看起来像这样:

<!doctype html>
<html lang="se">
    <head>
        <meta charset="utf-8" />
        <title>Title of This Web Page</title>
        <script src="scripts\jquery-1.8.2.js" type="text/javascript"></script>
        <script src="scripts\js.js" type="text/javascript"></script>
    </head>

    <body>
              <div id="test">
              </div>
    </body>

</html>

JSON 如下所示:

{

    "data": {
        "current_condition": [ … ],
        "request": [ … ],
        "weather": [
            {
                "date": "2012-10-17",
                "precipMM": "0.8",
                "tempMaxC": "11",
                "tempMaxF": "51",
                "tempMinC": "8",
                "tempMinF": "46",
                "weatherCode": "119",
                "weatherDesc": [
                    {
                        "value": "Cloudy"
                    }
                ],
                "weatherIconUrl": [
                    {
                        "value": "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0003_white_cloud.png"
                    }
                ],
                "winddir16Point": "SW",
                "winddirDegree": "224",
                "winddirection": "SW",
                "windspeedKmph": "27",
                "windspeedMiles": "17"
            },
            { … }
        ]
    }

}

最佳答案

使用 JavaScript 控制台。阅读它给您的错误。

Uncaught SyntaxError: Unexpected token ;

终止语句,而不是表达式。从 this.weather[0].date;

中删除 ;

如果你解决了这个问题,那么你会得到:

XMLHttpRequest cannot load http://free.worldweatheronline.com/feed/weather.ashx?q=Stockholm&format=json&num_of_days=2&key=e8536d3a52101433121710. Origin http://fiddle.jshell.net is not allowed by Access-Control-Allow-Origin.

这是well covered on this site

关于javascript - 使用 jQuery 将 JSON 中的元素转换为 html 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12950182/

相关文章:

c# - XDocument 到 JSON,JsonProperties

java - JSON 到 Java : How to model lists of objects into generic object class whose object name from json is variable

javascript - 将 HTML 表单存储到变量

javascript - 如何在 NodeJs 中修改数组中的 JSON 键名称

javascript - 当我添加removeMarker函数时,每5秒在 map 上加载图钉不显示图标

arrays - 如何在 flutter 中解析复杂的 json

javascript - 创建 javascript 日期 UTC

javascript - 如何从函数返回一个值,使用该值来制作数学公式并将解决方案(键/值)推送到对象数组?

javascript - 搜索因输入 onBlur 而停止

javascript - 使用 typescript/javascript 将数组拆分为组