我找到了一个JSON serialization and deserialization to objects in Flutter 的例子,但是如何使用一个人员列表来实现,比如:
[
{
"name": "John",
"age": 30,
"cars": [
{
"name": "BMW",
"models": [
"320",
"X3",
"X5"
]
}
]
},
{
"name": "John",
"age": 30,
"cars": [
{
"name": "Ford",
"models": [
"Fiesta",
"Focus",
"Mustang"
]
}
]
}
]当我调用_person = serializers.deserializeWith(Person.serializer, JSON.decode(json));时,我得到这个错误:
The following _CastError was thrown attaching to the render tree:
type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String' in type cast where
_InternalLinkedHashMap is from dart:collection
String is from dart:core
String is from dart:core我创建了一个周围的Persons类:
abstract class Persons implements Built<Persons, PersonsBuilder> {
BuiltList<Person> get persons;
Persons._();
factory Persons([updates(PersonsBuilder b)]) = _$Persons;
static Serializer<Persons> get serializer => _$personsSerializer;
}并调用_person = serializers.deserializeWith(Persons.serializer, JSON.decode(json));,但错误是相同的。
如何(反)序列化Json对象列表?
发布于 2018-03-14 01:40:42
而不是
_person = serializers.deserializeWith(Person.serializer, JSON.decode(json));
试一试
_person = serializers.deserializeWith(Person.serializer, (JSON.decode(json) as List).first);
或
var personList = (JSON.decode(json) as List).map((j) => serializers.deserializeWith(Person.serializer, j)).toList();
https://stackoverflow.com/questions/49181724
复制相似问题