对于以下代码
var address = new Address(); address.street = 'N 34th'; address.city = 'Seattle'; var serialization = new Serialization() ..addRuleFor(address); String output = serialization.write(address);
我如何获得像这样的json输出:
address: {'street':'N 34th','city':'Seattle'}
{"roots":[{"__Ref":true,"rule":3,"object":0}],"data":[[],[],[["Seattle","N 34th"]]],"rules":"{\"roots\":[{\"__Ref\":true,\"rule\":1,\"object\":0}],\"data\":[[],[[{\"__Ref\":true,\"rule\":4,\"object\":0},{\"__Ref\":true,\"rule\":3,\"rule\":5,\"rule\":6,\"object\":0}]],[[],[\"city\",\"street\"]],[[]],\"rule\":2,\"object\":1},\"\",\"object\":2},\"rule\":7,[\"Address\"]],\"rules\":null}"}
解决方法
您可以使用
JsonObject for Dart,将其添加到您的pubspec.yaml文件中,然后运行pub install(工具 – > Pub Install)
dependencies: json_object: git: git://github.com/chrisbu/dartwatch-JsonObject.git
然后更改你的代码调用objectToJson:
import 'package:json_object/json_object.dart'; var address = new Address(); address.street = 'N 34th'; address.city = 'Seattle'; String output = objectToJson(address);
请注意,objectToJson需要镜像支持(反射功能),该镜像只支持Dart VM.截至2012-12-20,dart2js不起作用.