【发布时间】:2016-06-02 09:38:08
【问题描述】:
如何解析json数据。我需要从值weather.main和weather.description的json响应中提取数据并将它们显示在div中。我已经发布了json响应和js, jquery脚本
//this json response and i need to extract values of weather.main and weather.description
{
"coord":{
"lon":80.28,
"lat":13.09
},
"weather":[
{
"id":802,
"main":"Clouds",
"description":"scattered clouds",
"icon":"03d"
}
],
"base":"stations",
"main":{
"temp":308.15,
"pressure":1004,
"humidity":53,
"temp_min":308.15,
"temp_max":308.15
},
"visibility":7000,
"wind":{
"speed":3.6,
"deg":260
},
"clouds":{
"all":40
},
"dt":1464852600,
"sys":{
"type":1,
"id":7834,
"message":0.0103,
"country":"IN",
"sunrise":1464826282,
"sunset":1464872558
},
"id":1264527,
"name":"Chennai",
"cod":200
}
<script>
function loadweather(){
var q = document.getElementById("in").value;
var appid = "086a3e2bd775aac95a9b096b5233f049";
var url = 'http://api.openweathermap.org/data/2.5/weather?q=' + q + '&appid=' + appid + '&units=metric';
$.getJSON(url, function (data) { $('.temp').html('' + data.main.temp + '°C')});
alert("parse2");
//how to access this json data "weather->main"
$.getJSON(url, function (data) { $('.cityname').html('' + data.weather.main)});
}
</script>
如何解析json数据。我需要从值weather.main和weather.description的json响应中提取数据并将它们显示在div中。我已经发布了json响应和js, jquery脚本
【问题讨论】:
-
诸如此类的琐碎问题已经有了答案。我建议您最好先在搜索引擎上搜索问题,然后再将其发布到 SO。您将在搜索本身中获得大量 SO 的结果。
-
data.weather[0].main 将获取主要数据
-
哎呀小错误,谢谢@ShwethaU 54
标签: javascript jquery html json