gpt4 book ai didi

json - Dart 对象 -> JSON 字符串无法转换为 JSON

转载 作者:行者123 更新时间:2023-12-01 23:56:08 28 4
gpt4 key购买 nike

我正在尝试将对象序列化为 JSON,然后再次从 JSON 返回到对象。

这是应该为我提供正确 JSON 的代码片段:

 LoginRequest req = new LoginRequest();
req.username = username;
req.password = password;
req.created = 123456;
req.test = "KOTS";
print(req.toString());

我在控制台中看到的是这样的:

{} (:1)

在 pubspec.yaml 中,我将 json_object 作为依赖项导入:

environment:
sdk: '>=1.0.0 <2.0.0'
dependencies:
...
json_object: any

我有一个扩展 JsonObject 的基本消息类:

import 'package:json_object/json_object.dart';

class Message extends JsonObject {

int created = new DateTime.now().millisecondsSinceEpoch;

}

然后我有一个扩展 Message 的 LoginRequest:

import 'Message.dart';

class LoginRequest extends Message {

String _username;
String _password;
String test;

String get username => _username;
set username(String username) {
_username = username.trim();
}

String get password => _password;
set password(String password) {
_password = password.trim();
}

}

我以为只有基类会转换为Json,所以我又写了一个测试用例:

Message msg = new Message();
msg.created = 123456;
print(msg.toString());

这也是打印:

{} (:1)

调用 objectToJson 会执行相同的操作:

objectToJson(msg).then((jsonStr) => print(jsonStr));
objectToJson(req).then((jsonStr) => print(jsonStr));

输出:

{}
{}

删除 extends JsonObject 会导致上面的代码产生堆栈跟踪:

Exception: Uncaught Error: Converting object to an encodable object failed.
Stack Trace:
#0 _JsonStringifier.writeObject (dart:convert/json.dart:660)
#1 _JsonStringStringifier.printOn (dart:convert/json.dart:831)
#2 _JsonStringStringifier.stringify (dart:convert/json.dart:813)
#3 JsonEncoder.convert (dart:convert/json.dart:243)
#4 JsonCodec.encode (dart:convert/json.dart:141)
#5 login (package:falm/login-dialog.dart:47:22)
#6 Function.apply (dart:core-patch/function_patch.dart:28)
#7 GeneratedObjectAccessorService.invoke (package:smoke/static.dart:149:28)
#8 invoke (package:smoke/smoke.dart:43:41)
#9 HtmlElement&Polymer.dispatchMethod (package:polymer/src/instance.dart:1054:19)
#10 BindingDelegate&PolymerEventBindings.getEventHandler.<anonymous closure> (package:polymer/src/events.dart:82:32)
#11 _RootZone.runUnaryGuarded (dart:async/zone.dart:1093)
#12 _RootZone.bindUnaryCallback.<anonymous closure> (dart:async/zone.dart:1122)
#13 BindingDelegate&PolymerEventBindings.prepareEventBinding.<anonymous closure>.<anonymous closure> (package:polymer/src/events.dart:101:67)

Clicked Login (:1)
Exception: Uncaught Error: Class '_LocalClassMirror' has no instance getter 'getters'.

NoSuchMethodError: method not found: 'getters'
Receiver: Instance of '_LocalClassMirror'
Arguments: []
Stack Trace:
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:45)
#1 _serializeObject (package:json_object/src/mirror_based_serializer.dart:127:16)
#2 objectToSerializable (package:json_object/src/mirror_based_serializer.dart:53:21)
#3 objectToJson (package:json_object/src/mirror_based_serializer.dart:22:23)
#4 login (package:falm/login-dialog.dart:43:17)
#5 Function.apply (dart:core-patch/function_patch.dart:28)
#6 GeneratedObjectAccessorService.invoke (package:smoke/static.dart:149:28)
#7 invoke (package:smoke/smoke.dart:43:41)
#8 HtmlElement&Polymer.dispatchMethod (package:polymer/src/instance.dart:1054:19)
#9 BindingDelegate&PolymerEventBindings.getEventHandler.<anonymous closure> (package:polymer/src/events.dart:82:32)
#10 _RootZone.runUnaryGuarded (dart:async/zone.dart:1093)
#11 _RootZone.bindUnaryCallback.<anonymous closure> (dart:async/zone.dart:1122)
#12 BindingDelegate&PolymerEventBindings.prepareEventBinding.<anonymous closure>.<anonymous closure> (package:polymer/src/events.dart:101:67)

Clicked Login (:1)
Exception: Uncaught Error: Converting object to an encodable object failed.
Stack Trace:
#0 _JsonStringifier.writeObject (dart:convert/json.dart:660)
#1 _JsonStringStringifier.printOn (dart:convert/json.dart:831)
#2 _JsonStringStringifier.stringify (dart:convert/json.dart:813)
#3 JsonEncoder.convert (dart:convert/json.dart:243)
#4 JsonCodec.encode (dart:convert/json.dart:141)
#5 login (package:falm/login-dialog.dart:47:22)
#6 Function.apply (dart:core-patch/function_patch.dart:28)
#7 GeneratedObjectAccessorService.invoke (package:smoke/static.dart:149:28)
#8 invoke (package:smoke/smoke.dart:43:41)
#9 HtmlElement&Polymer.dispatchMethod (package:polymer/src/instance.dart:1054:19)
#10 BindingDelegate&PolymerEventBindings.getEventHandler.<anonymous closure> (package:polymer/src/events.dart:82:32)
#11 _RootZone.runUnaryGuarded (dart:async/zone.dart:1093)
#12 _RootZone.bindUnaryCallback.<anonymous closure> (dart:async/zone.dart:1122)
#13 BindingDelegate&PolymerEventBindings.prepareEventBinding.<anonymous closure>.<anonymous closure> (package:polymer/src/events.dart:101:67)

Clicked Login (:1)
Exception: Uncaught Error: Class '_LocalClassMirror' has no instance getter 'getters'.

NoSuchMethodError: method not found: 'getters'
Receiver: Instance of '_LocalClassMirror'
Arguments: []
Stack Trace:
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:45)
#1 _serializeObject (package:json_object/src/mirror_based_serializer.dart:127:16)
#2 objectToSerializable (package:json_object/src/mirror_based_serializer.dart:53:21)
#3 objectToJson (package:json_object/src/mirror_based_serializer.dart:22:23)
#4 login (package:falm/login-dialog.dart:44:17)
#5 Function.apply (dart:core-patch/function_patch.dart:28)
#6 GeneratedObjectAccessorService.invoke (package:smoke/static.dart:149:28)
#7 invoke (package:smoke/smoke.dart:43:41)
#8 HtmlElement&Polymer.dispatchMethod (package:polymer/src/instance.dart:1054:19)
#9 BindingDelegate&PolymerEventBindings.getEventHandler.<anonymous closure> (package:polymer/src/events.dart:82:32)
#10 _RootZone.runUnaryGuarded (dart:async/zone.dart:1093)
#11 _RootZone.bindUnaryCallback.<anonymous closure> (dart:async/zone.dart:1122)
#12 BindingDelegate&PolymerEventBindings.prepareEventBinding.<anonymous closure>.<anonymous closure> (package:polymer/src/events.dart:101:67)

导入 dart:convert 并使用 JSON.encode 的效果相同:

import 'dart:convert' show JSON;

...

print(JSON.encode(msg));
print(JSON.encode(req));

输出:

{}
{}

如果我删除 extends JsonObject,那么它会抛出堆栈跟踪:

Exception: Uncaught Error: Converting object to an encodable object failed.
Stack Trace:
#0 _JsonStringifier.writeObject (dart:convert/json.dart:660)
#1 _JsonStringStringifier.printOn (dart:convert/json.dart:831)
#2 _JsonStringStringifier.stringify (dart:convert/json.dart:813)
#3 JsonEncoder.convert (dart:convert/json.dart:243)
#4 JsonCodec.encode (dart:convert/json.dart:141)
#5 login (package:falm/login-dialog.dart:47:22)
#6 Function.apply (dart:core-patch/function_patch.dart:28)
#7 GeneratedObjectAccessorService.invoke (package:smoke/static.dart:149:28)
#8 invoke (package:smoke/smoke.dart:43:41)
#9 HtmlElement&Polymer.dispatchMethod (package:polymer/src/instance.dart:1054:19)
#10 BindingDelegate&PolymerEventBindings.getEventHandler.<anonymous closure> (package:polymer/src/events.dart:82:32)
#11 _RootZone.runUnaryGuarded (dart:async/zone.dart:1093)
#12 _RootZone.bindUnaryCallback.<anonymous closure> (dart:async/zone.dart:1122)
#13 BindingDelegate&PolymerEventBindings.prepareEventBinding.<anonymous closure>.<anonymous closure> (package:polymer/src/events.dart:101:67)

Clicked Login (:1)
Exception: Uncaught Error: Class '_LocalClassMirror' has no instance getter 'getters'.

NoSuchMethodError: method not found: 'getters'
Receiver: Instance of '_LocalClassMirror'
Arguments: []
Stack Trace:
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:45)
#1 _serializeObject (package:json_object/src/mirror_based_serializer.dart:127:16)
#2 objectToSerializable (package:json_object/src/mirror_based_serializer.dart:53:21)
#3 objectToJson (package:json_object/src/mirror_based_serializer.dart:22:23)
#4 login (package:falm/login-dialog.dart:43:17)
#5 Function.apply (dart:core-patch/function_patch.dart:28)
#6 GeneratedObjectAccessorService.invoke (package:smoke/static.dart:149:28)
#7 invoke (package:smoke/smoke.dart:43:41)
#8 HtmlElement&Polymer.dispatchMethod (package:polymer/src/instance.dart:1054:19)
#9 BindingDelegate&PolymerEventBindings.getEventHandler.<anonymous closure> (package:polymer/src/events.dart:82:32)
#10 _RootZone.runUnaryGuarded (dart:async/zone.dart:1093)
#11 _RootZone.bindUnaryCallback.<anonymous closure> (dart:async/zone.dart:1122)
#12 BindingDelegate&PolymerEventBindings.prepareEventBinding.<anonymous closure>.<anonymous closure> (package:polymer/src/events.dart:101:67)

Clicked Login (:1)
Exception: Uncaught Error: Converting object to an encodable object failed.
Stack Trace:
#0 _JsonStringifier.writeObject (dart:convert/json.dart:660)
#1 _JsonStringStringifier.printOn (dart:convert/json.dart:831)
#2 _JsonStringStringifier.stringify (dart:convert/json.dart:813)
#3 JsonEncoder.convert (dart:convert/json.dart:243)
#4 JsonCodec.encode (dart:convert/json.dart:141)
#5 login (package:falm/login-dialog.dart:47:22)
#6 Function.apply (dart:core-patch/function_patch.dart:28)
#7 GeneratedObjectAccessorService.invoke (package:smoke/static.dart:149:28)
#8 invoke (package:smoke/smoke.dart:43:41)
#9 HtmlElement&Polymer.dispatchMethod (package:polymer/src/instance.dart:1054:19)
#10 BindingDelegate&PolymerEventBindings.getEventHandler.<anonymous closure> (package:polymer/src/events.dart:82:32)
#11 _RootZone.runUnaryGuarded (dart:async/zone.dart:1093)
#12 _RootZone.bindUnaryCallback.<anonymous closure> (dart:async/zone.dart:1122)
#13 BindingDelegate&PolymerEventBindings.prepareEventBinding.<anonymous closure>.<anonymous closure> (package:polymer/src/events.dart:101:67)

JsonObject 仍然是将对象序列化为 JSON 并将 JSON 反序列化为对象的正确方法吗? (我在代码中看到了copyright 2013,现在已经很古老了)。如果是这样,我是否在类里面错过了一些东西?

在 dart 食谱中有一些示例,其中每个类都有自己的 toJson 方法,并且所有值都手动复制到映射中,这很麻烦,如果我的整个应用程序是 JSON 驱动的,那么我将花费最多的钱我花时间编写样板 toJson/fromJson 方法 - 这正是我想要摆脱的,因此选择 dart 的原因。

此外,我在 StackOverflow 上看到了使用镜像的示例,后面是 dart2js 不完全支持镜像的评论 - 因为这是一个基于浏览器的应用程序,所以它可以编译为 javascript 至关重要。

更新:

根据罗伯特的回答,似乎做样板是不可避免的:

消息.dart

import 'dart:convert' show JSON;

class Message {

int created = new DateTime.now().millisecondsSinceEpoch;

Map toJson() {
Map map = new Map();
map["created"] = this.created;
return map;
}

String toString(){
return JSON.encode(this);
}

}

登录请求.dart

import 'Message.dart';

class LoginRequest extends Message {

String _username;
String _password;
String test;

String get username => _username;
set username(String username) {
_username = username.trim();
}

String get password => _password;
set password(String password) {
_password = password.trim();
}

Map toJson() {
Map map = super.toJson();
map["username"] = this.username;
map["password"] = this.password;
return map;
}

}

测试代码:

LoginRequest req = new LoginRequest();
req.username = username;
req.password = password;
req.created = 123456;
req.test = "KOTS";
print(req);
// outputs: {"created":123456,"username":"asdfasdf","password":"adfasdf"} (:1)

Message msg = new Message();
msg.created = 123456;
print(msg);
// outputs: {"created":123456} (:1)

toString 我只需要在 Message 类上实现一次,toJson 将需要在每个类上实现。

最佳答案

我认为你的问题是你使用了print(req.toString());。您是否尝试过这样做:

objectToJson(req).then((jsonStr) => print(jsonStr)); 也许这会给你一个 json 字符串。

我个人觉得你实际上应该提供一个 toJson/toObject 方法,因为你可以完全控制序列化的内容以及序列化的方式(例如排除私有(private)字段、空值等)。

//编辑

因为您使用 extends JsonObject toString() 返回一个空映射。

//编辑

https://code.google.com/p/dart/issues/detail?id=6490看起来它没有得到很好的支持。但无论如何你都不能使用 json_object 包。

异常:未捕获错误:类“_LocalClassMirror”没有实例 getter“getters”。这应该是因为该软件包需要实验性镜像功能。

使用JSON.encode您无法将对象放入其中。请参阅https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart:convert.JsonCodec#id_encode :

指定第二个参数或实现 .toJson()

关于json - Dart 对象 -> JSON 字符串无法转换为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27739434/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com