如何通过JSON响应走路吗? [英] How to walk through json response?
问题描述
我有这样的JSON响应,我想walke认为它得到了天气条件,如湿和temp_C等。我尝试了一些方法,但没有奏效。
({数据:{current_condition:[{cloudcover:50,
湿度:44,
observation_time:上午12点10分
precipMM:0.0,
pressure:1013
temp_C:-2,
temp_F:29,
可见性:16,
气象code:116
weatherDesc:[{值:晴间多云}],
weatherIconUrl:[{值:http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png}],
winddir16Point:W
winddirDegree:280,
windspeedKmph:24,
windspeedMiles:15
}],
要求:[{查询:罗切斯特,美利坚合众国
类型:城
}],
天气预报:[{日期:2012-02-25,
precipMM:2.2,
tempMaxC:-1,
tempMaxF:31,
tempMinC:-5,
tempMinF:24,
气象code:116
weatherDesc:[{值:晴间多云}],
weatherIconUrl:[{值:http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png}],
winddir16Point:W
winddirDegree:281
winddirection:W
windspeedKmph:54
windspeedMiles:34
}]
}})
我尝试这些:
$。的getJSON(urlFromMyAPI,功能(数据){
警报(data.current_condition.temp_C);
警报(data.temp_C);
警报(数据[current_condition] .temp_C);
//我也用循环
对于(i = 0; I< = 3;我++){
警报(data.current_condition [I])
}
});
};
我觉得你的主要问题是,你的数据是嵌套在一个名为对象的内部数据
,所以你需要一个参考额外级别进去了。这也是一个容易得多,看看当你格式化你这样的反应,使你可以更清楚地看到嵌套对象和数组你有什么:
({数据:{
current_condition:[
{
cloudcover:50,
湿度:44,
observation_time:上午12点10分
precipMM:0.0,
pressure:1013
temp_C:-2,
temp_F:29,
可见性:16,
气象code:116
weatherDesc:[
{值:晴间多云}
]
weatherIconUrl:[
{值:HTTP:\\ / \\ / www.worldweatheronline.com \\ /影像\\ / wsymbols01_png_64 \\ /wsymbol_0004_black_low_cloud.png}
]
winddir16Point:W
winddirDegree:280,
windspeedKmph:24,
windspeedMiles:15
}
]
请求:
{查询:罗切斯特,美利坚合众国,类型:城}
]
天气预报:[
{
日期:2012-02-25,
precipMM:2.2,
tempMaxC:-1,
tempMaxF:31,
tempMinC:-5,
tempMinF:24,
气象code:116
weatherDesc:[
{值:晴间多云}
]
weatherIconUrl:[
{值:HTTP:\\ / \\ / www.worldweatheronline.com \\ /影像\\ / wsymbols01_png_64 \\ /wsymbol_0002_sunny_intervals.png}
]
winddir16Point:W
winddirDegree:281
winddirection:W
windspeedKmph:54
windspeedMiles:34
}
]
}
})
这是说,如果你想获得目前的状况 temp_C
,它会是这样(请注意我对你的匿名函数,使$ C更改参数名$ C减少混乱):
$。的getJSON(urlFromMyAPI,函数(响应){
变种临时= response.data.current_condition [0] .temp_C;
});
如果你想临时为数字,你可能需要做:
$。的getJSON(urlFromMyAPI,函数(响应){
变种临时= parseInt函数(response.data.current_condition [0] .temp_C,10);
});
I have this json response and I'm trying to walke thought it to get the weather conditions such as "humidity" and "temp_C" and so on. I tried some ways but didn't work.
({ "data" : { "current_condition" : [ { "cloudcover" : "50",
"humidity" : "44",
"observation_time" : "12:10 AM",
"precipMM" : "0.0",
"pressure" : "1013",
"temp_C" : "-2",
"temp_F" : "29",
"visibility" : "16",
"weatherCode" : "116",
"weatherDesc" : [ { "value" : "Partly Cloudy" } ],
"weatherIconUrl" : [ { "value" : "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png" } ],
"winddir16Point" : "W",
"winddirDegree" : "280",
"windspeedKmph" : "24",
"windspeedMiles" : "15"
} ],
"request" : [ { "query" : "Rochester, United States Of America",
"type" : "City"
} ],
"weather" : [ { "date" : "2012-02-25",
"precipMM" : "2.2",
"tempMaxC" : "-1",
"tempMaxF" : "31",
"tempMinC" : "-5",
"tempMinF" : "24",
"weatherCode" : "116",
"weatherDesc" : [ { "value" : "Partly Cloudy" } ],
"weatherIconUrl" : [ { "value" : "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png" } ],
"winddir16Point" : "W",
"winddirDegree" : "281",
"winddirection" : "W",
"windspeedKmph" : "54",
"windspeedMiles" : "34"
} ]
} })
I tried these:
$.getJSON(urlFromMyAPI, function (data) {
alert(data.current_condition.temp_C);
alert(data.temp_C);
alert(data[current_condition].temp_C);
// I also use loop
for (i = 0; i <= 3; i++) {
alert(data.current_condition[i])
}
});
};
I think your main issue is that your data is nested inside an object named data
so you need an extra level of reference to get inside it. It's also a lot easier to see what you've got when you format your response like this so you can see the nested objects and arrays more clearly:
({ "data": {
"current_condition": [
{
"cloudcover": "50",
"humidity": "44",
"observation_time": "12:10 AM",
"precipMM": "0.0",
"pressure": "1013",
"temp_C": "-2",
"temp_F": "29",
"visibility": "16",
"weatherCode": "116",
"weatherDesc": [
{"value": "Partly Cloudy" }
],
"weatherIconUrl": [
{"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0004_black_low_cloud.png" }
],
"winddir16Point": "W",
"winddirDegree": "280",
"windspeedKmph": "24",
"windspeedMiles": "15"
}
],
"request": [
{"query": "Rochester, United States Of America", "type": "City" }
],
"weather": [
{
"date": "2012-02-25",
"precipMM": "2.2",
"tempMaxC": "-1",
"tempMaxF": "31",
"tempMinC": "-5",
"tempMinF": "24",
"weatherCode": "116",
"weatherDesc": [
{"value": "Partly Cloudy" }
],
"weatherIconUrl": [
{"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0002_sunny_intervals.png" }
],
"winddir16Point": "W",
"winddirDegree": "281",
"winddirection": "W",
"windspeedKmph": "54",
"windspeedMiles": "34"
}
]
}
})
That said, if you want to get the current condition temp_C
, it would be like this (note I changed the argument name for your anonymous function to make the code less confusing):
$.getJSON( urlFromMyAPI, function(response){
var temp = response.data.current_condition[0].temp_C;
});
If you want the temp as a number, you may need to do this:
$.getJSON( urlFromMyAPI, function(response){
var temp = parseInt(response.data.current_condition[0].temp_C, 10);
});
这篇关于如何通过JSON响应走路吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!