json - 我的代码中有什么错误,如何成功解析 json 中的数据?
问题描述
我正在 Flutter 中开发天气应用程序,遇到了一个问题,当我们在搜索栏中输入城市名称并单击下一个图标时,它会在屏幕上显示该特定地点的天气。但为了确保正确解析数据,我只在控制台中调用数据。尽管代码没有显示任何错误,但控制台中会引发多个错误。谁能告诉我如何解决此错误并成功解析此数据?
控制台中显示的错误如下:
An Observatory debugger and profiler on Chrome is available at: http://127.0.0.1:50548/vYt04KYoAA8=
The Flutter DevTools debugger and profiler on Chrome is available at:
http://127.0.0.1:9101?uri=http://127.0.0.1:50548/vYt04KYoAA8=
{"cod":"400","message":"Nothing to geocode"}
Error: Exception: Failed to get posts
at Object.throw_ [as throw] (http://localhost:50501/dart_sdk.js:5061:11)
at getWeather (http://localhost:50501/packages/finalproject/Weatherproj/data_service.dart.lib.js:31:21)
at getWeather.next (<anonymous>)
at http://localhost:50501/dart_sdk.js:38640:33
at _RootZone.runUnary (http://localhost:50501/dart_sdk.js:38511:59)
at _FutureListener.thenAwait.handleValue (http://localhost:50501/dart_sdk.js:33713:29)
at handleValueCallback (http://localhost:50501/dart_sdk.js:34265:49)
at Function._propagateToListeners (http://localhost:50501/dart_sdk.js:34303:17)
at _Future.new.[_completeWithValue] (http://localhost:50501/dart_sdk.js:34151:23)
at async._AsyncCallbackEntry.new.callback (http://localhost:50501/dart_sdk.js:34172:35)
at Object._microtaskLoop (http://localhost:50501/dart_sdk.js:38778:13)
at _startMicrotaskLoop (http://localhost:50501/dart_sdk.js:38784:13)
at http://localhost:50501/dart_sdk.js:34519:9
下面是模型类的代码:
class WeatherAppDetails {
City? city;
String? cod;
double? message;
int? cnt;
List<ListDetails>? list;
WeatherAppDetails(
{required this.city,
required this.cod,
required this.message,
required this.cnt,
required this.list});
WeatherAppDetails.fromJson(Map<String, dynamic> json) {
final cod = json['cod'];
final message = json['message'];
final cnt = json['cnt'];
if (json['list'] != null) {
list = <ListDetails>[];
json['list'].forEach((v) {
list!.add(ListDetails.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
if (city != null) {
data['city'] = city!.toJson();
}
data['cod'] = cod;
data['message'] = message;
data['cnt'] = cnt;
if (list != null) {
data['list'] = list!.map((v) => v.toJson()).toList();
}
return data;
}
}
class City {
int? id;
String? name;
Coord? coord;
String? country;
int? population;
int? timezone;
City(
{required this.id,
required this.name,
required this.coord,
required this.country,
required this.population,
required this.timezone});
City.fromJson(Map<String, dynamic> json) {
id = json['id'];
name = json['name'];
coord = (json['coord'] != null ? Coord.fromJson(json['coord']) : null)!;
country = json['country'];
population = json['population'];
timezone = json['timezone'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['id'] = id;
data['name'] = name;
if (coord != null) {
data['coord'] = coord!.toJson();
}
data['country'] = country;
data['population'] = population;
data['timezone'] = timezone;
return data;
}
}
class Coord {
double? lon;
double? lat;
Coord({required this.lon, required this.lat});
Coord.fromJson(Map<String, dynamic> json) {
lon = json['lon'];
lat = json['lat'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['lon'] = lon;
data['lat'] = lat;
return data;
}
}
class ListDetails {
int? dt;
int? sunrise;
int? sunset;
Temp? temp;
FeelsLike? feelsLike;
int? pressure;
int? humidity;
List<WeatherDetails>? weather;
double? speed;
int? deg;
double? gust;
int? clouds;
double? pop;
double? rain;
ListDetails(
{required this.dt,
required this.sunrise,
required this.sunset,
required this.temp,
required this.feelsLike,
required this.pressure,
required this.humidity,
required this.weather,
required this.speed,
required this.deg,
required this.gust,
required this.clouds,
required this.pop,
required this.rain});
ListDetails.fromJson(Map<String, dynamic> json) {
dt = json['dt'];
sunrise = json['sunrise'];
sunset = json['sunset'];
temp = json['temp'] != null ? Temp.fromJson(json['temp']) : null;
feelsLike = json['feels_like'] != null
? FeelsLike.fromJson(json['feels_like'])
: null;
pressure = json['pressure'];
humidity = json['humidity'];
if (json['weather'] != null) {
weather = <WeatherDetails>[];
json['weather'].forEach((v) {
weather!.add(WeatherDetails.fromJson(v));
});
}
speed = json['speed'];
deg = json['deg'];
gust = json['gust'];
clouds = json['clouds'];
pop = json['pop'];
rain = json['rain'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['dt'] = dt;
data['sunrise'] = sunrise;
data['sunset'] = sunset;
if (temp != null) {
data['temp'] = temp!.toJson();
}
if (feelsLike != null) {
data['feels_like'] = feelsLike!.toJson();
}
data['pressure'] = pressure;
data['humidity'] = humidity;
if (weather != null) {
data['weather'] = weather!.map((v) => v.toJson()).toList();
}
data['speed'] = speed;
data['deg'] = deg;
data['gust'] = gust;
data['clouds'] = clouds;
data['pop'] = pop;
data['rain'] = rain;
return data;
}
}
class Temp {
double? day;
double? min;
double? max;
double? night;
double? eve;
double? morn;
Temp(
{required this.day,
required this.min,
required this.max,
required this.night,
required this.eve,
required this.morn});
Temp.fromJson(Map<String, dynamic> json) {
day = json['day'];
min = json['min'];
max = json['max'];
night = json['night'];
eve = json['eve'];
morn = json['morn'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['day'] = day;
data['min'] = min;
data['max'] = max;
data['night'] = night;
data['eve'] = eve;
data['morn'] = morn;
return data;
}
}
class FeelsLike {
double? day;
double? night;
double? eve;
double? morn;
FeelsLike(
{required this.day,
required this.night,
required this.eve,
required this.morn});
FeelsLike.fromJson(Map<String, dynamic> json) {
day = json['day'];
night = json['night'];
eve = json['eve'];
morn = json['morn'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['day'] = day;
data['night'] = night;
data['eve'] = eve;
data['morn'] = morn;
return data;
}
}
class WeatherDetails {
int? id;
String? main;
String? description;
String? icon;
WeatherDetails(
{required this.id,
required this.main,
required this.description,
required this.icon});
WeatherDetails.fromJson(Map<String, dynamic> json) {
id = json['id'];
main = json['main'];
description = json['description'];
icon = json['icon'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['id'] = id;
data['main'] = main;
data['description'] = description;
data['icon'] = icon;
return data;
}
}
下面是网络类的代码:
class DataService {
Future<WeatherAppDetails> getWeather(String name) async {
final queryParameters = {
'q': name,
'appid': '3c044b7295d2df14f8bee74c19d4b96f',
'units': 'imperial',
'cnt': '7'
};
final uri = Uri.https(
'api.openweathermap.org', '/data/2.5/forecast/daily', queryParameters);
final response = await http.get(uri);
print(response.body);
if (response.statusCode == 200) {
return WeatherAppDetails.fromJson(jsonDecode(response.body));
} else {
throw Exception('Failed to get posts');
}
}
}
解决方案
基本上 api.weather 告诉你“q”参数的给定值不存在,没有找到地理位置。
添加城市参数输入很有帮助
您在请求中输入的城市错误,这应该有效(示例)
推荐阅读
- python - 如何访问另一个python文件中的变量
- django - pip install locustio ==0.8a2 没有安装?
- php - Laravel Validator 上的 Exists 子句错误消息不起作用
- swift - 在主线程上使用 @Published 值?
- sql - 获取前一行的 ID,该行在相关行之前并不总是静态行数?
- reactjs - React UseRef 的当前值既是初始值,也是我期望的元素
- python - 使用 JDBC 从数据框源复制到 SQL Server 目标
- python - 在字典中创建字典
- javascript - Angular CLI 删除箭头功能?
- pandas - 使用 table=true 格式化 Pandas 图中的表格