如何打印JSON颤振值?



我有一个JSON与一些键和值,我想访问我的小部件中的值,但我不明白如何。我为我的数据创建了一个类,但是如何访问这个值呢?

我的类:

class PersonKpi {
late Lc lc;
late Fc fc;
late Revenue revenue;
late Fc checklist;
late Persondata persondata;
late Total total;
PersonKpi(
{
required this.lc,
required this.fc,
required this.revenue,
required this.checklist,
required this.persondata,
required this.total
}
);
PersonKpi.fromJson(Map<String, dynamic> json) {
lc = (json['lc'] != null ? new Lc.fromJson(json['lc']) : null)!;
fc = (json['fc'] != null ? new Fc.fromJson(json['fc']) : null)!;
revenue =
(json['revenue'] != null ? new Revenue.fromJson(json['revenue']) : null)!;
checklist =
(json['checklist'] != null ? new Fc.fromJson(json['checklist']) : null)!;
persondata = (json['persondata'] != null
? new Persondata.fromJson(json['persondata'])
: null)!;
total = (json['total'] != null ? new Total.fromJson(json['total']) : null)!;
}
}
class Lc {
late int percent;
late String color;
late int weight;
late double totalWeight;
late double totalRubForPerson;
Lc({
required this.percent,
required this.color,
required this.weight,
required this.totalWeight,
required this.totalRubForPerson
});
Lc.fromJson(Map<String, dynamic> json) {
percent = json['percent'];
color = json['color'];
weight = json['weight'];
totalWeight = json['total_weight'];
totalRubForPerson = json['total_rub_for_person'];
}
}

和JSON:

{"lc" {"percent" 100.4,"color"yellow"weight" 30,"total_weight" 32.51,"total_rub_for_person" 7.5}}

如何打印Lc.percent?

请尝试这个,如果它来自API调用,那么你解码你的数据

void main() {
var mapdata = { "lc": { "percent": 100.4, "color": "yellow", "weight": 30, "total_weight": 32.51, "total_rub_for_person": 7.5 } };
// for data convert 
//var data = jsonDecode(mapdata.data);
print(PersonKpi.fromJson(mapdata).lc.percent); // 100
}

class PersonKpi {
Lc lc;
PersonKpi({this.lc});
PersonKpi.fromJson(Map<String, dynamic> json) {
lc = json['lc'] != null ? new Lc.fromJson(json['lc']) : null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.lc != null) {
data['lc'] = this.lc.toJson();
}
return data;
}
}
class Lc {
double percent;
String color;
int weight;
double totalWeight;
double totalRubForPerson;
Lc(
{this.percent,
this.color,
this.weight,
this.totalWeight,
this.totalRubForPerson});
Lc.fromJson(Map<String, dynamic> json) {
percent = json['percent'];
color = json['color'];
weight = json['weight'];
totalWeight = json['total_weight'];
totalRubForPerson = json['total_rub_for_person'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['percent'] = this.percent;
data['color'] = this.color;
data['weight'] = this.weight;
data['total_weight'] = this.totalWeight;
data['total_rub_for_person'] = this.totalRubForPerson;
return data;
}
}

最新更新