如何从API检索信息?

时间:2015-06-25 20:40:41

标签: javascript json api

如何从http://weather.gccdaily.com/api.php?city=dubai

获取该信息

创建如下信息:

{"apiVersion":"1.0", "data":{ "location":"dubai", "temperature":"24", "skytext":"Few clouds", "humidity":"47", "wind":"11.16 km/h", "date":"06-25-2015", "day":"Thursday" } }

如何在网站上显示温度,天空等。

2 个答案:

答案 0 :(得分:0)

您标记了此jquery:尝试getJson

$.getJSON( "http://weather.gccdaily.com/api.php?city=dubai", function( data ) {
  data = data.data;
  var items = [];
  $.each( data, function( key, val ) {
    items.push( "<li id='" + key + "'>" + val + "</li>" );
  });

  $( "<ul/>", {
    "class": "my-new-list",
    html: items.join( "" )
  }).appendTo( "body" );
});

答案 1 :(得分:0)

这是Json你可以像这样访问它:

    <p id="Weather"></p>

<script>
<!-- Paste or call your JSON here -->
var text = '{"apiVersion":"1.0", "data":{ "location":"Dubai", "temperature":"33", "skytext":"Sky is Clear", "humidity":"38", "wind":"14.76 km/h", "date":"06-25-2015", "day":"Thursday" } }';

obj = JSON.parse(text);

document.getElementById("Weather").innerHTML =

obj.data.location + "<br />"  +
obj.data.temperature + "<br />" +
obj.data.skytext + "<br />" +
obj.data.humidity + "<br />" +
obj.data.wind + "<br />" +
obj.data.date + "<br />" +
obj.data.day;

</script>

这是一个工作小提琴链接:

[http://jsfiddle.net/8875z02L/1/][1]