json - 如何从 Flutter 中的 Dio 请求中获取所有字段
问题描述
我正在使用 Dio 从这个 Api 获取请求:Api Example
响应如下:
{
"count": 87,
"next": "https://swapi.co/api/people/?page=2",
"previous": null,
"results": [
{
"name": "Luke Skywalker",
"height": "172",
"mass": "77",
"hair_color": "blond",
"skin_color": "fair",
"eye_color": "blue",
"birth_year": "19BBY",
"gender": "male",
"homeworld": "https://swapi.co/api/planets/1/",
"films": [
"https://swapi.co/api/films/2/",
"https://swapi.co/api/films/6/",
"https://swapi.co/api/films/3/",
"https://swapi.co/api/films/1/",
"https://swapi.co/api/films/7/"
],
"species": [
"https://swapi.co/api/species/1/"
],
"vehicles": [
"https://swapi.co/api/vehicles/14/",
"https://swapi.co/api/vehicles/30/"
],
"starships": [
"https://swapi.co/api/starships/12/",
"https://swapi.co/api/starships/22/"
],
"created": "2014-12-09T13:50:51.644000Z",
"edited": "2014-12-20T21:17:56.891000Z",
"url": "https://swapi.co/api/people/1/"
},
我在飞镖中有这个代码:
void _getNames() async {
final response = await dio.get('https://swapi.co/api/people');
List tempList = new List();
for (int i = 0; i < response.data['results'].length; i++) {
tempList.add(response.data['results'][i]);
}
setState(() {
names = tempList;
names.shuffle();
filteredNames = names;
});
}
使用此代码,我只能获得结果的名称,但我不知道如何获得其他字段,我尝试了一些东西但没有任何效果。我知道这很容易,但我不知道该怎么做。
解决方案
干得好
_getPeople() async {
var response = await http.get('https://swapi.co/api/people/');
if (response != null && response.statusCode == 200) {
ResultModel jsonResponse =
ResultModel.fromJson(convert.jsonDecode(response.body));
print(jsonResponse);
}
}
结果模型代码是
class ResultModel {
int count;
String next;
dynamic previous;
List<Result> results;
ResultModel({
this.count,
this.next,
this.previous,
this.results,
});
factory ResultModel.fromJson(Map<String, dynamic> json) {
return ResultModel(
count: json['count'],
next: json['next'],
previous: json['previous'],
results: _parseResult(json['results']),
);
}
}
_parseResult(List<dynamic> data) {
List<Result> results = new List<Result>();
data.forEach((item) {
results.add(Result.fromJson(item));
});
return results;
}
_parseString(List<dynamic> data) {
List<String> results = new List<String>();
data.forEach((item) {
results.add(item);
});
return results;
}
class Result {
String name;
String height;
String mass;
String hairColor;
String skinColor;
String eyeColor;
String birthYear;
String gender;
String homeworld;
List<String> films;
List<String> species;
List<String> vehicles;
List<String> starships;
String created;
String edited;
String url;
Result({
this.name,
this.height,
this.mass,
this.hairColor,
this.skinColor,
this.eyeColor,
this.birthYear,
this.gender,
this.homeworld,
this.films,
this.species,
this.vehicles,
this.starships,
this.created,
this.edited,
this.url,
});
factory Result.fromJson(Map<String, dynamic> json) {
return Result(
name: json['name'],
height: json['height'],
mass: json['mass'],
hairColor: json['hairColor'],
skinColor: json['skinColor'],
eyeColor: json['eyeColor'],
birthYear: json['birthYear'],
gender: json['gender'],
homeworld: json['homeworld'],
films: _parseString(json['films']),
species: _parseString(json['species']),
vehicles: _parseString(json['vehicles']),
created: json['created'],
edited: json['edited'],
url: json['url'],
);
}
}
推荐阅读
- php - Docker & Laravel:配置:错误:未满足包要求(oniguruma)
- eclipse - PydevPlugin - 在新版本中设置 PythonInterpreterManager
- sql-server - 存储的数据显然在没有警告的情况下消失了
- c - 链接器是否查找头文件
- bash - 在 bash shell 中使用 for 循环和 sed 拆分字符串
- java - LDAP 用户注册给出“没有全局高级知识”错误,而 DN 是根据 .ldif 文件定义的
- python - findChessboardCorners 无法找到超过 3x3 的棋盘
- node.js - 有没有办法通过发送您知道将被请求的数据来减少获取请求“浪费”的时间?
- python - 使用 py2neo 不返回节点的所有关系
- c# - 我可以编译数据集以便不需要原始数据库吗?[c#, .net 框架]