如何获取JSON序列化字符串使用Dart序列化 [英] How to get JSON serialized string using Dart Serialization
本文介绍了如何获取JSON序列化字符串使用Dart序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于以下代码
var address = new Address();
address.street ='N 34th'
address.city ='Seattle';
var serialization = new Serialization()
..addRuleFor(address);
String output = serialization.write(address);
如何获得如下的json输出:
地址:{'street':'N 34th','city':'Seattle'}
上述代码生成的输出如下:
{roots: [[],[],[],[[Seattle,N 34th]]],[数据]:[{__Ref:true,rule:3,object:0} rules:{\roots \:[{\__ Ref\:true,\rule\:1,\object \:0}],\ data \:[[],[[{\__ Ref\:true,\rule\:4,\object \:0},{\__ Ref\\ \\:true,\rule\:3,\object \:0},{\__ Ref\:true,\rule\:5,\ object \:0},{\__ Ref\:true,\rule\:6,\object \:0}],[[],[] \city\,\street\],[[]],[[]],[[]],[[{\__ Ref\:true,\ \:2,\object \:0},{\__ Ref\:true,\rule\:2,\object \:1} \\\,{\__ Ref\:true,\rule\:2,\object \:2},{\__ Ref\:true, \\rule \:7,\object \:0}]],[\Address\]],\rules\:null}}
解决方案您可以使用 JsonObject for Dart ,将它添加到您的
pubspec.yaml
文件,然后运行pub install
(工具 - > Pub安装)依赖项:
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。For the following code
var address = new Address(); address.street = 'N 34th'; address.city = 'Seattle'; var serialization = new Serialization() ..addRuleFor(address); String output = serialization.write(address);
How do i get a json output like this:
address: {'street':'N 34th', 'city':'Seattle'}
Output generated by above code is as follows:
{"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,\"object\":0},{\"__Ref\":true,\"rule\":5,\"object\":0},{\"__Ref\":true,\"rule\":6,\"object\":0}]],[[],[],[\"city\",\"street\"]],[[]],[[]],[[]],[[{\"__Ref\":true,\"rule\":2,\"object\":0},{\"__Ref\":true,\"rule\":2,\"object\":1},\"\",{\"__Ref\":true,\"rule\":2,\"object\":2},{\"__Ref\":true,\"rule\":7,\"object\":0}]],[\"Address\"]],\"rules\":null}"}
解决方案You can use the JsonObject for Dart, add this to your
pubspec.yaml
file and then runpub install
(Tools -> Pub Install)dependencies: json_object: git: git://github.com/chrisbu/dartwatch-JsonObject.git
And then change your code to call
objectToJson
:import 'package:json_object/json_object.dart'; var address = new Address(); address.street = 'N 34th'; address.city = 'Seattle'; String output = objectToJson(address);
Note that
objectToJson
requires mirrors support (the reflection capabilities) which only work in Dart VM at the moment. It does not work in dart2js as of 2012-12-20.这篇关于如何获取JSON序列化字符串使用Dart序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文