如何在Flutter中将对象编码为json [英] How to encode an object to json in Flutter
问题描述
我正在尝试将对象 Week转换为json。
I am trying to convert the object "Week" to json.
https:// flutter.dev/docs/development/data-and-backend/json 这是我使用的来源
class Week{
DateTime _startDate;
DateTime _endDate;
List<Goal> _goalList;
String _improvement;
Week(this._startDate, this._endDate){
this._goalList = List<Goal>();
this._improvement = "";
}
Week.fromJson(Map<String, dynamic> json)
: _startDate = json['startDate'],
_endDate = json['endDate'],
_goalList = json['goalList'],
_improvement = json['improvement'];
Map<String, dynamic> toJson() =>
{
'startDate': _startDate,
'endDate': _endDate,
'goalList': _goalList,
'improvement': _improvement,
};
}
我用到了:
DateTime startDate = currentDate.subtract(new Duration(days:(weekday-1)));
DateTime endDate = currentDate.add(new Duration(days:(7-weekday)));
Week week = new Week(startDate, endDate);
var json = jsonEncode(week);
但是问题是我得到了以下结果:
But the problem is that I get this result:
Unhandled Exception: Converting object to an encodable object failed: Instance of 'Week'
#0 _JsonStringifier.writeObject (dart:convert/json.dart:647:7)
#1 _JsonStringStringifier.printOn (dart:convert/json.dart:834:17)
#2 _JsonStringStringifier.stringify (dart:convert/json.dart:819:5)
#3 JsonEncoder.convert (dart:convert/json.dart:255:30)
#4 JsonCodec.encode (dart:convert/json.dart:166:45)
#5 jsonEncode (dart:convert/json.dart:80:10)
推荐答案
jsonEncode要求 Map< String,dynamic>
,而不是 Week
对象。调用您的 toJson()
方法应该可以解决问题。
jsonEncode requires a Map<String, dynamic>
, not a Week
object. Calling your toJson()
method should do the trick.
var json = jsonEncode(week.toJson());
但是,请记住,您的 toJson()
方法也不正确,因为_goalList之类的东西和日期仍然是对象,而不是Maps或Lists。您还需要在这些方法上实现toJson方法。
However, keep in mind that your toJson()
method is also incorrect, as things like _goalList and the dates are still objects, not Maps or Lists. You'll need to implement toJson methods on those as well.
要回答您的特定问题:
- 因为dart不是javascript /打字稿。 Dart会在运行时检查类型,因此您必须明确告诉它如何转换事物-dart中也没有反射,因此无法自行解决。
- 您可以使用使用代码生成功能的库来自动为您执行这些操作-尽管在运行时仍然无法实现-阅读有关 JSON序列化。
- 最简单的方法是直接在类中实现方法,因为这是您可以在根对象中访问的地方。请记住,
jsonEncode
所需的结构是Map< String,dynamic>
,但dynamic
部分实际上意味着List< dynamic>
,Map< String,dynamic>
或与json兼容的原语,例如String
或double
-如果您尝试想象这样的嵌套结构,所说的类型看起来,您将意识到它基本上是json。因此,当您执行'goalList':_goalList,
之类的操作时,会为其提供一个对象,该对象不是允许的类型之一。
- Because dart is not javascript / typescript. Dart checks types at runtime, therefore you have to explicitly tell it how to convert things - also there is no reflection in dart, so it can't figure it out by itself.
- You can use a library that uses code generation to do these things automatically for you - it still wont be possible at runtime though - read more about JSON serialization.
- The easiest way would be to implement the methods directly in the classes, as that's where you have access to in your root object. Keep in mind that the structure that
jsonEncode
needs is aMap<String, dynamic>
, but thedynamic
part really meansList<dynamic>
,Map<String, dynamic>
or a primitive that is json compatible such asString
ordouble
- if you try to imagine how such a nested structure of said types looks, you'll realise that it's basically json. So when you do something like'goalList': _goalList,
you're giving it an object, which is not one of the allowed types.
希望这可以使事情变得更简单。
Hope this clears things up a bit.
这篇关于如何在Flutter中将对象编码为json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!