json - Flutter 将自定义堆叠对象转换为 json
问题描述
所以我有一个相对复杂的对象结构,我想把它保存在 SQFLite 中。但是由于这个对象有其他对象的列表,我想我会将子对象列表转换为 json 并将其保存为文本。
像这样:
"name": "String",
"Body": {
"Object1": [
{
"index": int,
}
],
"Object2": [
{
"index":2,
}
]
}
当我按下按钮时,会创建一个新对象并将其添加到数据库中。(原始插入)
但是出现了这个问题:
Unhandled Exception: DatabaseException(java.lang.String cannot be cast to java.lang.Integer)
据我了解,这意味着将字符串转换为 int 时出错。
完整的错误代码
E/flutter (20088): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: DatabaseException(java.lang.String cannot be cast to java.lang.Integer) sql 'INSERT Into workoutPlan (name,id,workoutDays,pastId,timesDone,workoutBody) VALUES (?,?,?,?,?,?)' args [nummero uno, 2, [Monday, Friday], 345, 45, {Pause: [{timeInMilSec: 900, index: 5}], exercise: [{goal: 2, weightGoal: [15, 15, 15], name: PushUp, timeGoal: 900, index: 1, repGoal: 3, setGoal: [infinity, 15, 3]}]}]}
我的模型课:
import 'dart:convert';
PlanModel planModelFromJson(String str) => PlanModel.fromJson(json.decode(str));
String planModelToJson(PlanModel data) => json.encode(data.toJson());
class PlanModel {
PlanModel({
this.name,
this.id,
this.workoutDays,
this.pastId,
this.timesDone,
this.workoutBody,
});
String name;
int id;
List<String> workoutDays;
int pastId;
int timesDone;
WorkoutBody workoutBody;
factory PlanModel.fromJson(Map<String, dynamic> json) => PlanModel(
name: json["name"],
id: json["id"],
workoutDays: List<String>.from(json["workoutDays"].map((x) => x)),
pastId: json["pastId"],
timesDone: json["timesDone"],
workoutBody: WorkoutBody.fromJson(json["workoutBody"]),
);
Map<String, dynamic> toJson() => {
"name": name,
"id": id,
"workoutDays": List<dynamic>.from(workoutDays.map((x) => x)),
"pastId": pastId,
"timesDone": timesDone,
"workoutBody": workoutBody.toJson(),
};
}
class WorkoutBody {
WorkoutBody({
this.exercise,
this.pause,
});
List<Exercise> exercise;
List<Pause> pause;
factory WorkoutBody.fromJson(Map<String, dynamic> json) => WorkoutBody(
exercise: List<Exercise>.from(json["exercise"].map((x) => Exercise.fromJson(x))),
pause: List<Pause>.from(json["Pause"].map((x) => Pause.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"exercise": List<dynamic>.from(exercise.map((x) => x.toJson())),
"Pause": List<dynamic>.from(pause.map((x) => x.toJson())),
};
}
class Exercise {
Exercise({
this.index,
this.name,
this.goal,
this.repGoal,
this.weightGoal,
this.timeGoal,
this.setGoal,
});
int index;
String name;
int goal;
int repGoal;
List<int> weightGoal;
int timeGoal;
List<String> setGoal;
factory Exercise.fromJson(Map<String, dynamic> json) => Exercise(
index: json["index"],
name: json["name"],
goal: json["goal"],
repGoal: json["repGoal"],
weightGoal: List<int>.from(json["weightGoal"].map((x) => x)),
timeGoal: json["timeGoal"],
setGoal: List<String>.from(json["setGoal"].map((x) => x)),
);
Map<String, dynamic> toJson() => {
"index": index,
"name": name,
"goal": goal,
"repGoal": repGoal,
"weightGoal": List<dynamic>.from(weightGoal.map((x) => x)),
"timeGoal": timeGoal,
"setGoal": List<dynamic>.from(setGoal.map((x) => x)),
};
}
class Pause {
Pause({
this.index,
this.timeInMilSec,
});
int index;
int timeInMilSec;
factory Pause.fromJson(Map<String, dynamic> json) => Pause(
index: json["index"],
timeInMilSec: json["timeInMilSec"],
);
Map<String, dynamic> toJson() => {
"index": index,
"timeInMilSec": timeInMilSec,
};
}
解决方案
我发现了问题:)
在 planModel toJson() 我有
"workoutDays": List<dynamic>.from(workoutDays.map((x) => x)),
并将其更改为jsonDecode(workoutDays)
并再次阅读我使用jsonEncode(src)
推荐阅读
- c# - 在内部范围的调度程序上等待运行后如何强制代码?
- c - C - 当我从文件中读取矩阵时,为什么前几个元素会失真?
- ios - Fabric Crashlytics 崩溃状态视图中显示的版本号是什么?
- spring-boot - 通量
- 或 Flux in spring webflux
- python - 如何让这个脚本在后台运行并监听“复制”动作?
- python - 我想编辑用户输入的python代码
- c# - 以下数字赋值是否会导致 C# 中的隐式强制转换?
- f# - 为什么 None 在 F# 中自动更改为 null
- javascript - JQuery 没有进入第二个函数
- ruby-on-rails - ActiveAdmin 中的访问控制列表?