Javascript 如何在jquery中解析以下天气api
我想解析天气API(URL)城市名称、温度等 我的JSON数据如下所示:Javascript 如何在jquery中解析以下天气api,javascript,jquery,Javascript,Jquery,我想解析天气API(URL)城市名称、温度等 我的JSON数据如下所示: { "data": { "current_condition": [{ "cloudcover": "25", "humidity": "70", "observation_time": "04:21 PM", "precipMM": "0.3", "pressure": "100
{
"data": {
"current_condition": [{
"cloudcover": "25",
"humidity": "70",
"observation_time": "04:21 PM",
"precipMM": "0.3",
"pressure": "1007",
"temp_C": "30",
"temp_F": "86",
"visibility": "4",
"weatherCode": "113",
"weatherDesc": [{
"value": "Clear"}],
"weatherIconUrl": [{
"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0008_clear_sky_night.png"}],
"winddir16Point": "S",
"winddirDegree": "180",
"windspeedKmph": "7",
"windspeedMiles": "4"}],
"request": [{
"query": "Ahmedabad, India",
"type": "City"}],
"weather": [{
"date": "2012-09-18",
"precipMM": "2.1",
"tempMaxC": "32",
"tempMaxF": "89",
"tempMinC": "25",
"tempMinF": "76",
"weatherCode": "176",
"weatherDesc": [{
"value": "Patchy rain nearby"}],
"weatherIconUrl": [{
"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0009_light_rain_showers.png"}],
"winddir16Point": "SSW",
"winddirDegree": "203",
"winddirection": "SSW",
"windspeedKmph": "12",
"windspeedMiles": "8"},
{
"date": "2012-09-19",
"precipMM": "3.4",
"tempMaxC": "32",
"tempMaxF": "89",
"tempMinC": "25",
"tempMinF": "76",
"weatherCode": "176",
"weatherDesc": [{
"value": "Patchy rain nearby"}],
"weatherIconUrl": [{
"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0009_light_rain_showers.png"}],
"winddir16Point": "SW",
"winddirDegree": "223",
"winddirection": "SW",
"windspeedKmph": "12",
"windspeedMiles": "7"}]
}
}
我如何解析这些数据并获得城市名称和温度..我不知道..非常感谢
===================输出=======================
我想获取像这样的数据并设置Textbox
Date 2012-09-18 2012-09-19
tempMaxC 32 32
tempMinC 25 25
tempMaxF 89 89
tempMinF 76 76
如果已将此JSON作为字符串检索,请将此字符串传递给
JSON.parse()
*,然后访问常规JavaScript对象的检索值:
var jsonStr = '{ "data": { "current_condition": [ {"cloudcover": "25", "humidity": "70", "observation_time": "04:21 PM", "precipMM": "0.3", "pressure": "1007", "temp_C": "30", "temp_F": "86", "visibility": "4", "weatherCode": "113", "weatherDesc": [ {"value": "Clear" } ], "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0008_clear_sky_night.png" } ], "winddir16Point": "S", "winddirDegree": "180", "windspeedKmph": "7", "windspeedMiles": "4" } ], "request": [ {"query": "Ahmedabad, India", "type": "City" } ], "weather": [ {"date": "2012-09-18", "precipMM": "2.1", "tempMaxC": "32", "tempMaxF": "89", "tempMinC": "25", "tempMinF": "76", "weatherCode": "176", "weatherDesc": [ {"value": "Patchy rain nearby" } ], "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0009_light_rain_showers.png" } ], "winddir16Point": "SSW", "winddirDegree": "203", "winddirection": "SSW", "windspeedKmph": "12", "windspeedMiles": "8" }, {"date": "2012-09-19", "precipMM": "3.4", "tempMaxC": "32", "tempMaxF": "89", "tempMinC": "25", "tempMinF": "76", "weatherCode": "176", "weatherDesc": [ {"value": "Patchy rain nearby" } ], "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0009_light_rain_showers.png" } ], "winddir16Point": "SW", "winddirDegree": "223", "winddirection": "SW", "windspeedKmph": "12", "windspeedMiles": "7" } ] }}',
jsonObj = JSON.parse(jsonStr);
console.log(jsonObj.data.current_condition[0].temp_F);
否则,如果您已检索此JSON,例如作为某个jQuery$.ajax()
成功回调的参数,并且它已经是一个对象,则无需调用JSON.parse()
,只需直接检索对象的值:
$.getJSON("http://example.com/weather.json", function(jsonObj) {
// The response string is already parsed with $.parseJSON(),
// so you don't need to parse it yourself.
// Therefore just go ahead and access the properties of JavaScript object.
console.log(jsonObj.data.current_condition[0].temp_F);
});
*如果您打算支持不支持JSON.parse/stringify的旧浏览器(如IE7),则需要包括
更新:
对于特定情况如果您已将此JSON作为字符串检索,则将此字符串传递给
JSON.parse()
*,然后访问常规JavaScript对象的检索值:
var jsonStr = '{ "data": { "current_condition": [ {"cloudcover": "25", "humidity": "70", "observation_time": "04:21 PM", "precipMM": "0.3", "pressure": "1007", "temp_C": "30", "temp_F": "86", "visibility": "4", "weatherCode": "113", "weatherDesc": [ {"value": "Clear" } ], "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0008_clear_sky_night.png" } ], "winddir16Point": "S", "winddirDegree": "180", "windspeedKmph": "7", "windspeedMiles": "4" } ], "request": [ {"query": "Ahmedabad, India", "type": "City" } ], "weather": [ {"date": "2012-09-18", "precipMM": "2.1", "tempMaxC": "32", "tempMaxF": "89", "tempMinC": "25", "tempMinF": "76", "weatherCode": "176", "weatherDesc": [ {"value": "Patchy rain nearby" } ], "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0009_light_rain_showers.png" } ], "winddir16Point": "SSW", "winddirDegree": "203", "winddirection": "SSW", "windspeedKmph": "12", "windspeedMiles": "8" }, {"date": "2012-09-19", "precipMM": "3.4", "tempMaxC": "32", "tempMaxF": "89", "tempMinC": "25", "tempMinF": "76", "weatherCode": "176", "weatherDesc": [ {"value": "Patchy rain nearby" } ], "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0009_light_rain_showers.png" } ], "winddir16Point": "SW", "winddirDegree": "223", "winddirection": "SW", "windspeedKmph": "12", "windspeedMiles": "7" } ] }}',
jsonObj = JSON.parse(jsonStr);
console.log(jsonObj.data.current_condition[0].temp_F);
否则,如果您已检索此JSON,例如作为某个jQuery$.ajax()
成功回调的参数,并且它已经是一个对象,则无需调用JSON.parse()
,只需直接检索对象的值:
$.getJSON("http://example.com/weather.json", function(jsonObj) {
// The response string is already parsed with $.parseJSON(),
// so you don't need to parse it yourself.
// Therefore just go ahead and access the properties of JavaScript object.
console.log(jsonObj.data.current_condition[0].temp_F);
});
*如果您打算支持不支持JSON.parse/stringify的旧浏览器(如IE7),则需要包括
更新:
对于特殊情况您尝试过什么?告诉我们你的想法。顺便说一句:你是如何得到JSON的,它是否已经被解析成JS对象了?如果你格式化了数据,你将能够找到它->我们希望你自己尝试解决这个问题,而不是要求社区为你找到一个完整的解决方案。当您有一些代码向我们展示您的一些努力时(即使是错误的),请更新您的问题和标志以重新打开。谢谢。你试过什么了?告诉我们你的想法。顺便说一句:你是如何得到JSON的,它是否已经被解析成JS对象了?如果你格式化了数据,你将能够找到它->我们希望你自己尝试解决这个问题,而不是要求社区为你找到一个完整的解决方案。当您有一些代码向我们展示您的一些努力时(即使是错误的),请更新您的问题和标志以重新打开。谢谢。我编辑了我的问题并编写了输出格式。我想要这样的输出。请再次阅读问题并给我支持。请检查我的演示()以获得想法。非常感谢各位,不客气。很高兴能提供帮助。您好..请再来一个查询。我是jquery和javascript新手,所以请帮助我..我想在下面的URL中传递城市名称动态,并获取json数据,如表vise…我编辑我的问题并编写输出格式。我想要这样的输出。请再次阅读问题并给我支持。请检查我的演示()以获得想法。非常感谢各位,不客气。很高兴能提供帮助。您好..请再查询一次。我是jquery和javascript新手,所以请帮助我..我想在下面的URL中传递城市名称动态,并获取json数据,如表vise。。。