jquery - javascript json ajax数据解析

标签 jquery html css json ajax

我是 ajax 和 jquery 编程的新手。我正在设计天气网络应用程序,它在网站上显示某个城市的天气。我已经能够在网站上成功显示天气。但现在我想从 JSON 中获取温度并将其转换为华氏温度。这是我的网络应用程序代码

        $(function(){


     $('#clickme').click(function() {
         $("#weather").empty();

        var search_City2 = $("#c1").val();

        $.ajax({
          url: 'http://localhost:3000/?id='+search_City2,
          dataType: 'json',
          success: function(data) {
             var items = [];

             $.each(data, function(key, val) {

               items.push('<li id="' + key + '">' + key + ' : '+ val + '</li>');

             });

             $('<ul/>', {
                'class': 'weather-list',
                html: items.join('')
             }).appendTo('#weather');
             var city = search_City2;


          },
         statusCode: {
            404: function() {
              alert('There was a problem with the server.  Try again soon!');
            }
          }
       });
     });
 });

这是我的 JSON 数据

{ text: 'Light Rain',
  temp: '12',
  date: 'Fri, 27 Nov 2015 11:00 am EST',
  high: '14',
  low: '-2' }

最佳答案

试一试。

var jsObj = jQuery.parseJSON(data); // Convert string JSON to object

if (jsObj.temp) {

    var temp = parseFloat(jsObj.temp.toString());
    var tempFahrenheit = temp * 33.8; // 1 Celcuis = 33.8 Fahrenheit

    console.log('Temperature: ' + tempFahrenheit + 'ºF');
}

关于jquery - javascript json ajax数据解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33962379/

相关文章:

javascript - 从indexeddb中删除特定存储

javascript - setInterval 触发过于频繁 (Javascript)

html - 具有 z-index 的元素内图像的 z-index

html - 背景和相对定位 html 和 css 问题

jquery - 使用 CSS 和 jQuery 的横向滚动菜单

javascript - IE 不在删除时触发输入事件

JQuery:采取特定操作时防止触发其他事件

html - CSS - 溢出-x : scroll cuts off beginning of div

iphone - @font-face 在移动 Webkit 中不工作

html - "text-decoration: none"不工作,即使是 "!important"