我问了一个question https://stackoverflow.com/questions/25615241/dart-json-decoder然而,在关于 Dart 编码/解码到 JSON 之前,建议的库并不完整,我决定手动处理它。
目标是将这些对象转换为地图。
class Parent extends Object {
int id;
String name;
List<Child> listChild = new List<Child>();
Map toMap() => {"id":id, "name":name, "listChild":listChild};
}
class Child extends Object {
int id;
String childName;
Map toMap() => {"id":id, "childName":childName};
}
做的时候
print(JSON.encode(parent.toMap()));
我看到它在这里,有什么建议如何使其工作吗?
if (!stringifyJsonValue(object)) {
checkCycle(object);
try {
var customJson = _toEncodable(object);
if (!stringifyJsonValue(customJson)) {
throw new JsonUnsupportedObjectError(object);
}
_removeSeen(object);
} catch (e) {
throw new JsonUnsupportedObjectError(object, cause : e);
}
}
}
Map toMap() => {"id":id, "name":name: "listChild": listChild.map((c) => c.toJson().toList())};
对 JSON 有效。
import 'dart:convert' show JSON;
...
String json = JSON.encode(toMap());
您还可以使用toEncodeable
回调-参见如何将 DateTime 对象转换为 json https://stackoverflow.com/questions/21813942
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)