当我进行取回请求时,接收到的数据是映射类型,但我需要能够将其转换为列表类型,我该如何做?
当我进行取回请求时,接收到的数据是映射类型,但我需要能够将其转换为列表类型,我该怎么做?
Future<AltinModel> altinGetir() async {
String url = 'https://finans.truncgil.com/v2/today.json';
final res = await http.get(Uri.parse(url));
if (res.statusCode != 200) {
throw Exception('error');
}
var altin = utf8.decode(res.body.runes.toList());
var js = json.decode(altin);
var gram_altin = json.decode(res.body);
return AltinModel.fromJson(js);
}
模型类模型类模型类模型类
// To parse this JSON data, do
//
// final altinModel = altinModelFromJson(jsonString);
import 'dart:convert';
AltinModel altinModelFromJson(String str) =>
AltinModel.fromJson(json.decode(str));
String altinModelToJson(AltinModel data) => json.encode(data.toJson());
class AltinModel {
AltinModel({
required this.updateDate,
required this.onsAltin,
required this.gramAltin,
required this.ceyrekAltin,
required this.yarimAltin,
required this.tamAltin,
required this.cumhuriyetAltini,
required this.ataAltin,
required this.resatAltin,
required this.hamitAltin,
required this.ikibucukAltin,
required this.gremseAltin,
required this.besliAltin,
required this.the14AyarAltin,
required this.the18AyarAltin,
required this.the22AyarBilezik,
required this.gumus,
});
DateTime updateDate;
The14AyarAltin onsAltin;
The14AyarAltin gramAltin;
The14AyarAltin ceyrekAltin;
The14AyarAltin yarimAltin;
The14AyarAltin tamAltin;
The14AyarAltin cumhuriyetAltini;
The14AyarAltin ataAltin;
The14AyarAltin resatAltin;
The14AyarAltin hamitAltin;
The14AyarAltin ikibucukAltin;
The14AyarAltin gremseAltin;
The14AyarAltin besliAltin;
The14AyarAltin the14AyarAltin;
The14AyarAltin the18AyarAltin;
The14AyarAltin the22AyarBilezik;
The14AyarAltin gumus;
factory AltinModel.fromJson(Map<String, dynamic> json) => AltinModel(
updateDate: DateTime.parse(json["Update_Date"]),
onsAltin: The14AyarAltin.fromJson(json["ons_altin"]),
gramAltin: The14AyarAltin.fromJson(json["gram_altin"]),
ceyrekAltin: The14AyarAltin.fromJson(json["ceyrek_altin"]),
yarimAltin: The14AyarAltin.fromJson(json["yarim_altin"]),
tamAltin: The14AyarAltin.fromJson(json["tam_altin"]),
cumhuriyetAltini: The14AyarAltin.fromJson(json["cumhuriyet_altini"]),
ataAltin: The14AyarAltin.fromJson(json["ata_altin"]),
resatAltin: The14AyarAltin.fromJson(json["resat_altin"]),
hamitAltin: The14AyarAltin.fromJson(json["hamit_altin"]),
ikibucukAltin: The14AyarAltin.fromJson(json["ikibucuk_altin"]),
gremseAltin: The14AyarAltin.fromJson(json["gremse_altin"]),
besliAltin: The14AyarAltin.fromJson(json["besli_altin"]),
the14AyarAltin: The14AyarAltin.fromJson(json["14_ayar_altin"]),
the18AyarAltin: The14AyarAltin.fromJson(json["18_ayar_altin"]),
the22AyarBilezik: The14AyarAltin.fromJson(json["22_ayar_bilezik"]),
gumus: The14AyarAltin.fromJson(json["gumus"]),
);
Map<String, dynamic> toJson() => {
"Update_Date": updateDate.toIso8601String(),
"ons_altin": onsAltin.toJson(),
"gram_altin": gramAltin.toJson(),
"ceyrek_altin": ceyrekAltin.toJson(),
"yarim_altin": yarimAltin.toJson(),
"tam_altin": tamAltin.toJson(),
"cumhuriyet_altini": cumhuriyetAltini.toJson(),
"ata_altin": ataAltin.toJson(),
"resat_altin": resatAltin.toJson(),
"hamit_altin": hamitAltin.toJson(),
"ikibucuk_altin": ikibucukAltin.toJson(),
"gremse_altin": gremseAltin.toJson(),
"besli_altin": besliAltin.toJson(),
"14_ayar_altin": the14AyarAltin.toJson(),
"18_ayar_altin": the18AyarAltin.toJson(),
"22_ayar_bilezik": the22AyarBilezik.toJson(),
"gumus": gumus.toJson(),
};
}
class The14AyarAltin {
The14AyarAltin({
required this.buying,
required this.selling,
required this.type,
required this.name,
});
String buying;
String selling;
String type;
String name;
factory The14AyarAltin.fromJson(Map<String, dynamic> json) => The14AyarAltin(
buying: json["Buying"],
selling: json["Selling"],
type: json["Type"],
name: json["Name"],
);
Map<String, dynamic> toJson() => {
"Buying": buying,
"Selling": selling,
"Type": type,
"Name": name,
};
}
EDIT3:测试你的代码后,我现在看到你想做什么。
这是我对你的问题的回答,如果你不喜欢,请随时评论。
为了我自己的理解,我在这里改了一些名字。
The14AyarAlin成为我的模型类:
import 'package:stackoverflow/helper.dart' as helper;
class Altin {
Altin({
required this.key,
required this.buying,
required this.selling,
required this.type,
required this.name,
});
String key;
double buying;
double selling;
String type;
String name;
factory Altin.fromJson(String key, Map<String, dynamic> map) => Altin(
key : key,
buying: helper.stringToDouble(map["Buying"]),
selling: helper.stringToDouble(map["Selling"]),
type: map["Type"],
name: map["Name"],
);
Map<String, dynamic> toJson() => {
"Buying": helper.doubleToString(buying),
"Selling": helper.doubleToString(selling),
"Type": type,
"Name": name,
};
}
和你的AltinModel变成了AyarAtlins:
import 'package:stackoverflow/models/altin.dart';
class AyarAltins {
AyarAltins({
required this.updateDate,
required this.elements,
});
final DateTime updateDate;
final List<Altin> elements;
factory AyarAltins.fromJson(Map<String, dynamic> json) {
DateTime updateDate = DateTime.parse(json["Update_Date"]);
List<Altin> elements = [];
json.remove('Update_Date');
json.forEach((key, value) {
elements.add(Altin.fromJson(key, value));
});
return AyarAltins(
updateDate: updateDate,
elements: elements
);
}
Map<String, dynamic> toJson() {
Map<String, dynamic> map = {
"Update_Date": updateDate.toIso8601String()
};
for (var element in elements) {
map[element.key] = element.toJson();
}
return map;
}
}
我用
测试它Future getData() async {
AyarAltins ayarAltins = await altinGetir();
Map<String, dynamic> map = ayarAltins.toJson();
print('end');
}
Future<AyarAltins> altinGetir() async {
String url = 'https://finans.truncgil.com/v2/today.json';
final res = await http.get(Uri.parse(url));
if (res.statusCode != 200) {
throw Exception('error');
}
var altin = utf8.decode(res.body.runes.toList());
var js = json.decode(altin);
return AyarAltins.fromJson(js);
}
这里一切都很好。
为了更好地处理,我将一些值从string更改为double。为此,我使用了一个名为helper的文件。
double stringToDouble(String text) {
text = text.replaceAll('.', '');
text = text.replaceAll(',', '.');
return double.parse(text);
}
String doubleToString(double value) {
String text = value.toString();
if(text.contains('.')) {
List<String> split = text.split('.');
text = '${_comaFormat(split[0])}.${split[1]}';
} else {
text = _comaFormat(text);
}
return text;
}
String _comaFormat(String text){
String formatted = "";
int counter = 0;
for(int i = (text.length - 1); i >= 0; i--){
counter++;
String str = text[i];
if((counter % 3) != 0 && i !=0){
formatted = "$str$formatted";
}else if(i == 0 ){
formatted = "$str$formatted";
}else{
formatted = ",$str$formatted";
}
}
return formatted.trim();
}
要从新的ayaraltin中提取列表,现在可以使用,例如:
List<Altin> filteredList = ayarAltins.elements.where((element) => element.buying > 700).toList();
这里它将返回一个包含11个元素的列表,这些元素的购买值为>700 .