从Json API响应中正确建模数据



试图从我的API中获取数据。我能够从其他字段中获取数据,但不能用于"行为"。场。

这是我的json:

[
{
"_id": {
"$oid": "625f2900fe6aeb351381c3f5"
}, 
"breed": "Africanis", 
"origin": "Southern Africa", 
"url": "https://en.wikipedia.org/wiki/Africanis", 
"img": "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4b/Africanis_%281%29.jpg/220px-Africanis_%281%29.jpg", 
"WikiDescr": [
{
"contenido": "some content"
}
], 
"Behavior": [
{
"good_with_children": 3, 
"good_with_other_dogs": 1, 
"shedding": 3, 
"grooming": 3, 
"drooling": 1, 
"coat_length": 1, 
"good_with_strangers": 2, 
"playfulness": 3, 
"protectiveness": 5, 
"trainability": 3, 
"energy": 4, 
"barking": 2, 
"min_life_expectancy": 10, 
"max_life_expectancy": 14, 
"max_height_male": 28, 
"max_height_female": 28, 
"max_weight_male": 130, 
"max_weight_female": 100, 
"min_height_male": 26, 
"min_height_female": 26, 
"min_weight_male": 100, 
"min_weight_female": 70
}
], 
"url_alternative": []
}
]

这是我从https://app.quicktype.io/生成的飞镖模型:

class DogClass {
Id? _iId;
String? _breed;
String? _origin;
String? _url;
String? _img;
List<WikiDescr>? _wikiDescr;
List<Behavior>? _behavior;
List<dynamic>? _urlAlternative;
DogClass({
Id? iId,
String? breed,
String? origin,
String? url,
String? img,
List<WikiDescr>? wikiDescr,
List<Behavior>? behavior,
List<dynamic>? urlAlternative
}) {
if (iId != null) {
this._iId = iId;
}
if (breed != null) {
this._breed = breed;
}
if (origin != null) {
this._origin = origin;
}
if (url != null) {
this._url = url;
}
if (img != null) {
this._img = img;
}
if (img != null) {
this._img = img;
}
if (wikiDescr != null) {
this._wikiDescr = wikiDescr;
}
if (behavior != null) {
this._behavior = behavior;
}
if (urlAlternative != null) {
this._urlAlternative = urlAlternative;
}
}
Id? get iId => _iId;
set iId(Id? iId) => _iId = iId;
String? get breed => _breed;
set breed(String? breed) => _breed = breed;
String? get origin => _origin;
set origin(String? origin) => _origin = origin;
String? get url => _url;
set url(String? url) => _url = url;
String? get img => _img;
set img(String? img) => _img = img;
List<WikiDescr>? get wikiDescr => _wikiDescr;
set wikiDescr(List<WikiDescr>? wikiDescr) => _wikiDescr = wikiDescr;
List<Behavior>? get behavior => _behavior;
set behavior(List<Behavior>? behavior) => _behavior = behavior;

List<dynamic>? get urlAlternative => _urlAlternative;
set urlAlternative(List<dynamic>? urlAlternative) => _urlAlternative = urlAlternative;
factory DogClass.fromJson(Map<String, dynamic> json) {
return DogClass(
iId: json['_id'] == null
? null
: Id.fromJson(json['_id'] as Map<String, dynamic>),
breed: json['breed'] as String?,
origin: json['origin'] as String?,
url: json['url'] as String?,
img: json['img'] as String?,
wikiDescr: List<WikiDescr>.from(
json["WikiDescr"].map((x) => WikiDescr.fromJson(x))),
behavior: List<Behavior>.from(json["Behavior"].map((x) => Behavior.fromJson(x))),
urlAlternative: List<UrlAlternative>.from(json["url_alternative"].map((x) => UrlAlternative.fromJson(x)))
);
}
}
class Id {
String? _oid;
Id({String? oid}) {
if (oid != null) {
this._oid = oid;
}
}
String? get oid => _oid;
set oid(String? oid) => _oid = oid;
Id.fromJson(Map<String, dynamic> json) {
_oid = json['$oid'];
}
}
class WikiDescr {
WikiDescr({
required this.contenido,
});
String contenido;
factory WikiDescr.fromRawJson(String str) =>
WikiDescr.fromJson(json.decode(str));
String toRawJson() => json.encode(toJson());
factory WikiDescr.fromJson(Map<String, dynamic> json) => WikiDescr(
contenido: json["contenido"],
);
Map<String, dynamic> toJson() => {
"contenido": contenido,
};
}
class Behavior {
int? _goodWithChildren;
int? _goodWithOtherDogs;
int? _shedding;
int? _grooming;
int? _drooling;
int? _coatLength;
int? _goodWithStrangers;
int? _playfulness;
int? _protectiveness;
int? _trainability;
int? _energy;
int? _barking;
int? _minLifeExpectancy;
int? _maxLifeExpectancy;
int? _maxHeightMale;
int? _maxHeightFemale;
int? _maxWeightMale;
int? _maxWeightFemale;
int? _minHeightMale;
int? _minHeightFemale;
int? _minWeightMale;
int? _minWeightFemale;
Behavior(
{int? goodWithChildren,
int? goodWithOtherDogs,
int? shedding,
int? grooming,
int? drooling,
int? coatLength,
int? goodWithStrangers,
int? playfulness,
int? protectiveness,
int? trainability,
int? energy,
int? barking,
int? minLifeExpectancy,
int? maxLifeExpectancy,
int? maxHeightMale,
int? maxHeightFemale,
int? maxWeightMale,
int? maxWeightFemale,
int? minHeightMale,
int? minHeightFemale,
int? minWeightMale,
int? minWeightFemale}) {
if (goodWithChildren != null) {
this._goodWithChildren = goodWithChildren;
}
if (goodWithOtherDogs != null) {
this._goodWithOtherDogs = goodWithOtherDogs;
}
if (shedding != null) {
this._shedding = shedding;
}
if (grooming != null) {
this._grooming = grooming;
}
if (drooling != null) {
this._drooling = drooling;
}
if (coatLength != null) {
this._coatLength = coatLength;
}
if (goodWithStrangers != null) {
this._goodWithStrangers = goodWithStrangers;
}
if (playfulness != null) {
this._playfulness = playfulness;
}
if (protectiveness != null) {
this._protectiveness = protectiveness;
}
if (trainability != null) {
this._trainability = trainability;
}
if (energy != null) {
this._energy = energy;
}
if (barking != null) {
this._barking = barking;
}
if (minLifeExpectancy != null) {
this._minLifeExpectancy = minLifeExpectancy;
}
if (maxLifeExpectancy != null) {
this._maxLifeExpectancy = maxLifeExpectancy;
}
if (maxHeightMale != null) {
this._maxHeightMale = maxHeightMale;
}
if (maxHeightFemale != null) {
this._maxHeightFemale = maxHeightFemale;
}
if (maxWeightMale != null) {
this._maxWeightMale = maxWeightMale;
}
if (maxWeightFemale != null) {
this._maxWeightFemale = maxWeightFemale;
}
if (minHeightMale != null) {
this._minHeightMale = minHeightMale;
}
if (minHeightFemale != null) {
this._minHeightFemale = minHeightFemale;
}
if (minWeightMale != null) {
this._minWeightMale = minWeightMale;
}
if (minWeightFemale != null) {
this._minWeightFemale = minWeightFemale;
}
}
int? get goodWithChildren => _goodWithChildren;
set goodWithChildren(int? goodWithChildren) =>
_goodWithChildren = goodWithChildren;
int? get goodWithOtherDogs => _goodWithOtherDogs;
set goodWithOtherDogs(int? goodWithOtherDogs) =>
_goodWithOtherDogs = goodWithOtherDogs;
int? get shedding => _shedding;
set shedding(int? shedding) => _shedding = shedding;
int? get grooming => _grooming;
set grooming(int? grooming) => _grooming = grooming;
int? get drooling => _drooling;
set drooling(int? drooling) => _drooling = drooling;
int? get coatLength => _coatLength;
set coatLength(int? coatLength) => _coatLength = coatLength;
int? get goodWithStrangers => _goodWithStrangers;
set goodWithStrangers(int? goodWithStrangers) =>
_goodWithStrangers = goodWithStrangers;
int? get playfulness => _playfulness;
set playfulness(int? playfulness) => _playfulness = playfulness;
int? get protectiveness => _protectiveness;
set protectiveness(int? protectiveness) => _protectiveness = protectiveness;
int? get trainability => _trainability;
set trainability(int? trainability) => _trainability = trainability;
int? get energy => _energy;
set energy(int? energy) => _energy = energy;
int? get barking => _barking;
set barking(int? barking) => _barking = barking;
int? get minLifeExpectancy => _minLifeExpectancy;
set minLifeExpectancy(int? minLifeExpectancy) =>
_minLifeExpectancy = minLifeExpectancy;
int? get maxLifeExpectancy => _maxLifeExpectancy;
set maxLifeExpectancy(int? maxLifeExpectancy) =>
_maxLifeExpectancy = maxLifeExpectancy;
int? get maxHeightMale => _maxHeightMale;
set maxHeightMale(int? maxHeightMale) => _maxHeightMale = maxHeightMale;
int? get maxHeightFemale => _maxHeightFemale;
set maxHeightFemale(int? maxHeightFemale) =>
_maxHeightFemale = maxHeightFemale;
int? get maxWeightMale => _maxWeightMale;
set maxWeightMale(int? maxWeightMale) => _maxWeightMale = maxWeightMale;
int? get maxWeightFemale => _maxWeightFemale;
set maxWeightFemale(int? maxWeightFemale) =>
_maxWeightFemale = maxWeightFemale;
int? get minHeightMale => _minHeightMale;
set minHeightMale(int? minHeightMale) => _minHeightMale = minHeightMale;
int? get minHeightFemale => _minHeightFemale;
set minHeightFemale(int? minHeightFemale) =>
_minHeightFemale = minHeightFemale;
int? get minWeightMale => _minWeightMale;
set minWeightMale(int? minWeightMale) => _minWeightMale = minWeightMale;
int? get minWeightFemale => _minWeightFemale;
set minWeightFemale(int? minWeightFemale) =>
_minWeightFemale = minWeightFemale;
factory Behavior.fromJson(Map<String, int> json) =>Behavior(
goodWithChildren : json['good_with_children'],
goodWithOtherDogs : json['good_with_other_dogs'],
shedding : json['shedding'],
grooming : json['grooming'],
drooling : json['drooling'],
coatLength : json['coat_length'],
goodWithStrangers : json['good_with_strangers'],
playfulness : json['playfulness'],
protectiveness : json['protectiveness'],
trainability : json['trainability'],
energy : json['energy'],
barking : json['barking'],
minLifeExpectancy : json['min_life_expectancy'],
maxLifeExpectancy : json['max_life_expectancy'],
maxHeightMale : json['max_height_male'],
maxHeightFemale : json['max_height_female'],
maxWeightMale : json['max_weight_male'],
maxWeightFemale : json['max_weight_female'],
minHeightMale : json['min_height_male'],
minHeightFemale : json['min_height_female'],
minWeightMale : json['min_weight_male'],
minWeightFemale : json['min_weight_female']
);
}
class UrlAlternative {
UrlAlternative({
required this.urls,
});
List<String> urls;
factory UrlAlternative.fromJson(Map<String, dynamic> json) => UrlAlternative(
urls: List<String>.from(json["urls"].map((x) => x)),
);
}

请关注"行为"。字段。其余的工作正常。在我的工厂方法中是否缺少一些东西??

我宁愿只在需要时创建自己的类。当我的数据非常复杂时,我会使用生成器,而你的情况不是。

你需要首先处理原始数据,然后你可以使用类和方法,因为你知道它是如何结构的,以及如何处理它来获得关于它的某些信息,我想。

import 'dart:convert';
import 'package:_samples4/json/json_africa_wiki.dart';
typedef JMap = Map<String, dynamic>;
void main(List<String> args) {
var list = (jsonDecode(rawJsonAfrica) as List).cast<JMap>();
var data = list.first;
var behaviour = (data['Behavior'] as List).first as JMap;
print(behaviour.keys);
print(behaviour.values);
}

输出:

(good_with_children, good_with_other_dogs, shedding, ..., min_weight_male, min_weight_female)
(3, 1, 3, 3, 1, 1, 2, 3, 5, 3, 4, 2, 10, 14, 28, 28, 130, 100, 26, 26, 100, 70)

最新更新