繁体   English   中英

如何处理json响应并在div中显示值

[英]how to handle json response and display the values in a div

我正在使用openweather api获取天气数据。当我输入城市名称时,服务器返回一个json数据,我需要知道如何处理数据,并且需要在div内显示数据

 function loadweather() { var q = document.getElementById("in").value; var appid = document.getElementById("appid").value; var url = 'http://api.openweathermap.org/data/2.5/weather?q=' + q + '&appid=' + appid; $.getJSON(url, function(data) { console.log(data) }); } 
 <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script> <input type="text" id="in" value="New Delhi"/> <input type="hidden" id="appid" value="086a3e2bd775aac95a9b096b5233f049"> <button id="go" onclick="loadweather()">Search</button> <div id="disp"></div> 

您可以访问作为对象获得的数据。 在您的情况下,对象将如下所示。

public class RootObject
{
public Coord coord { get; set; }
public List<Weather> weather { get; set; }
public string @base { get; set; }
public Main main { get; set; }
public int visibility { get; set; }
public Wind wind { get; set; }
public Clouds clouds { get; set; }
public int dt { get; set; }
public Sys sys { get; set; }
public int id { get; set; }
public string name { get; set; }
public int cod { get; set; }

}

您可以如下所示访问它本身的脚本。

$ .getJSON(URL,function(data){$('#disp')。html('Temperature:'+ data.main.temp +'deg Fahrenheit'+'Pressure:'+ data.main.pressure)}) ;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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