我正在使用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/"
},
我在dart中有以下代码:
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;
});
}
使用此代码,我仅获得结果的名称,但是我不知道如何获取其他字段,我尝试了一些操作,但没有任何效果。我知道这很容易,但是我不知道该怎么办。
答案 0 :(得分:0)
你在这里
_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);
}
}
ResultModel代码为
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'],
);
}
}