gpt4 book ai didi

javascript - 如何在jquery中解析以下天气api

转载 作者:行者123 更新时间:2023-11-28 20:50:45 25 4
gpt4 key购买 nike

我想解析Weather API(URL)城市名称、温度等。

我的 JSON 数据如下:

{
"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"}]
}
}​

我如何解析这些数据并获取城市名称和温度..我不知道..提前致谢。

==============输出========================

我想获取这样的数据并在文本框中设置

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 library .

更新:

DEMO对于特定情况

关于javascript - 如何在jquery中解析以下天气api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12483318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com