[英]Flutter - Converting Json map of a map to List<Object> in flutter
我正在嘗試在父 class 中反序列化 json。 我的其中一個項目是 Map 的 Map,我想將其反序列化為 class 類型。 由於 map 中嵌套的 map ,我正在努力解析它,我不清楚正確的語法。 歸根結底,我想要父 class 中的列表。 json 類型塊中的項目是動態的,因此可能存在具有不同描述的關鍵、通知等類型。
Json 樣品:
{
"types":{
"alert":{
"description":"Action item."
},
"question":{
"description":"Select an applicable response."
},
"info":{
"description":"This is an information message, no action required."
}
}
}
class 類型:
class Types {
final String name;
final String description;
Types({
required this.name,
required this.description,
});
factory Types.fromJson(String id, Map<String, dynamic> json) {
return Types(
name: id,
description: json['description'] == null ? '' : json['description'],
);
}
}
父 class:
class Parent {
final String id;
final String name;
final String description;
final Features features;
final List<Types> types;
final List<String> users;
Parent({
required this.id,
required this.name,
required this.description,
required this.features,
required this.types,
required this.users,
});
factory Parent.fromJson( Map<String, dynamic> json) {
return Parent(
id: json['id'] == null ? '' : json['id'],
name: json['name'] == null ? '' : json['name'],
description: json['description'] == null ? '' : json['description'],
features: json['features'] == null
? Features()
: Features.fromJson(json['features']),
types: json['types'] == null ? [] : // ??? How to deserialize Map{Map{}} ,
users: json['users'] == null ? [] : List<String>.from(json['users']),
);
}
}
任何和所有的幫助表示贊賞。 同樣,如果有更好的方法來存儲這些數據,我對此持開放態度。 class 類型允許我在必要時添加未來的字段。
謝謝你。
總體思路是遍歷json['types']
中的每個鍵/值對,並為每個鍵/值創建一個Types
。
import 'dart:convert';
void main(List<String> args) {
final json = jsonDecode('''
{
"id": "test id",
"name": "test name",
"description": "test description",
"types":{
"alert":{
"description":"Action item."
},
"question":{
"description":"Select an applicable response."
},
"info":{
"description":"This is an information message, no action required."
}
}
}
''');
print(Parent.fromJson(json));
}
class Parent {
final String id;
final String name;
final String description;
final List<Types> types;
// ignoring features and users fields
Parent({
required this.id,
required this.name,
required this.description,
required this.types,
});
// prefer initializer lists to factory constructors
// when you are only creating instances of the same class
//
// also prefer:
// json['id'] ?? ''
// to:
// json['id'] == null ? '' : json['id']
//
Parent.fromJson(Map<String, dynamic> json)
: id = json['id'] ?? '',
name = json['name'] ?? '',
description = json['description'] ?? '',
// json['types'] is a map so you will have to loop
// over each of its entries (key/value pairs) and
// instantiate a new Types class for each entry.
types = [
for (final entry in (json['types'] ?? {}).entries)
Types.fromJson(entry.key, entry.value),
];
@override
String toString() =>
'Parent(id: $id, name: $name, description: $description, types: $types)';
}
class Types {
final String name;
final String description;
Types({
required this.name,
required this.description,
});
Types.fromJson(String id, Map<String, dynamic> json)
: name = id,
description = json['description'] ?? '';
@override
String toString() => 'Types(name: $name, description: $description)';
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.