簡體   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