#flutter
Вопрос:
У меня есть 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»: { «процент»: 100,4, «цвет»: «желтый», «вес»: 30, «общий вес»: 32,51, «total_rub_for_person»: 7,5 } }
Как напечатать Lc.процент?
Ответ №1:
Пожалуйста, попробуйте с этим, и если это вызов 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;
}
}