Jquery 使用json值使switch语句工作
我创建了一个switch语句,用于查找JSON对象的值,并基于该值向控制台提供数据。但是它不起作用。有人能告诉我我错过了什么吗Jquery 使用json值使switch语句工作,jquery,angularjs,Jquery,Angularjs,我创建了一个switch语句,用于查找JSON对象的值,并基于该值向控制台提供数据。但是它不起作用。有人能告诉我我错过了什么吗 .controller('GeoCtrl', function($scope, $http) { $scope.result = ""; $http.get('http://api.airvisual.com/v2/city?country=usa&state=arizona&city=phoenix&key=PxJe9btQYW
.controller('GeoCtrl', function($scope, $http) {
$scope.result = "";
$http.get('http://api.airvisual.com/v2/city?country=usa&state=arizona&city=phoenix&key=PxJe9btQYWReyQFAm')
.success(function(data, status, headers,config){
console.log('data success');
console.log(data); // for browser console
$scope.result = data; // for UI
})
.error(function(data, status, headers,config){
console.log('data error');
})
.then(function(result){
things = result.data;
val = $(things.data.current.pollution.aqius);
console.log(val);
var level;
var health;
var sport;
switch (true) {
case (val < 50):
level = "Good";
health = "Air quality is considered satisfactory, and air pollution poses little or no risk";
sport = "Perfect day to play!";
break;
case (51 <= val && val < 100):
level = "Moderate";
health = "Air quality is acceptable; however, for some pollutants there may be a moderate health concern for a very small number of people who are unusually sensitive to air pollution.";
sport = "Active children and adults, and people with respiratory disease, such as asthma, should limit prolonged outdoor exertion.";
break;
case (101 <= val && val < 150):
level = "Unhealthy for Sensitive Groups";
health = "Members of sensitive groups may experience health effects. The general public is not likely to be affected.";
sport = "Active children and adults, and people with respiratory disease, such as asthma, should avoid prolonged outdoor exertion; everyone else, especially children, should limit prolonged outdoor exertion";
break;
case (151 <= val && val < 200):
level = "Unhealthy";
health = "Unhealthy Everyone may begin to experience health effects; members of sensitive groups may experience more serious health effects";
sport = "Active children and adults, and people with respiratory disease, such as asthma, should avoid prolonged outdoor exertion; everyone else, especially children, should limit prolonged outdoor exertion";
break;
case (201 <= val && val < 300):
level = "Very Unhealthy";
health = "Health warnings of emergency conditions. The entire population is more likely to be affected.";
sport = "Active children and adults, and people with respiratory disease, such as asthma, should avoid all outdoor exertion; everyone else, especially children, should limit outdoor exertion.";
break;
case (301 <= val):
level = "Hazardous";
health = "Health alert: everyone may experience more serious health effects";
sport = "Everyone should avoid all outdoor exertion";
}
console.log(level);
console.log(health);
console.log(sport);
});
})
我可以使用console.log获取该值,但无法让switch语句对该值执行任何操作 最新答案:
查看您的代码,问题在于您要返回的承诺。我认为您可能对使用数据作为返回类型感到困惑。如果查看api返回的对象,将得到以下结果集:
{
"data": {
"status": "success",
"data": {
"city": "Phoenix",
"state": "Arizona",
"country": "USA",
"location": {
"type": "Point",
"coordinates": [
-112.066299,
33.560299
]
},
"current": {
"weather": {
"ts": "2017-07-17T06:00:00.000Z",
"tp": 25,
"pr": 1013,
"hu": 88,
"ws": 6,
"wd": 110,
"ic": "11n"
},
"pollution": {
"ts": "2017-07-17T05:00:00.000Z",
"aqius": 124,
"mainus": "p2",
"aqicn": 97,
"maincn": "p1"
}
}
}
},
"status": 200,
"config": {
"method": "GET",
"transformRequest": [
null
],
"transformResponse": [
null
],
"url": "http://api.airvisual.com/v2/city?country=usa&state=arizona&city=phoenix&key=PxJe9btQYWReyQFAm",
"headers": {
"Accept": "application/json, text/plain, */*"
}
},
"statusText": "OK"
}
因此,在代码中,您需要将结果设置更改为:
$scope.result = data.data.data;
var val = $scope.result.current.pollution.aqius;
这将纠正输出 角度。模块'myApp',[]
.controller'GeoCtrl',函数$scope,$http{
$scope.result=;
//简单的GET请求示例:
$http{
方法:“GET”,
网址:'http://api.airvisual.com/v2/city?country=usa&state=arizona&city=phoenix&key=PxJe9btQYWReyQFAm'
}.Then函数成功回调数据、状态、标题、配置{
$scope.result=数据;
var val=$scope.result.data.current.pollution.aqius;
console.logval;
var水平;
var健康;
体育;
开关val{
病例val<50:
水平=良好;
健康=认为空气质量令人满意,空气污染造成的风险很小或没有;
运动=完美的一天!;
打破
案例51但您从console.logval;?NaN将为所有语句返回false请更正标记。这不是angularjs问题,而是angularjs问题。如果$是jQuery,则val是jQuery对象,而不是数字。val=$things.data.current.pollution.aqius;应该做什么?此外,删除对象转换:val=Numberthings.data.current.pollution.aqius;请发布val的值。尝试使用parseInt确保val为int。同时保留一个默认情况,尽管如此。请参阅,很遗憾,它对我不起作用。控制台说污染未定义