如何将JavaScript对象转换为Dart Map? [英] How to convert JavaScript object to Dart Map?

查看:360
本文介绍了如何将JavaScript对象转换为Dart Map?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找新JsObject.jsify 的反向。东西,将javascript 对象转换回Dart 地图是否有可用的东西?

I am looking for some reverse of new JsObject.jsify. Something, that would convert javascript Object back to Dart Map Is there something like that available?

我知道我可以使用JSON转换为字符串,但这并不涉及包含函数,Dart对象,Dom Elements的 Object 的转移等等......有没有更好的方法?

I know that I can use JSON conversion to string, but this does not address transfer of Objects containing functions, Dart objects, Dom Elements, etc... Is there any better method?

推荐答案

如果你想深入处理并处理其他案例那么简单映射和保留函数(与json解决方案不同)然后使用这个简单的函数:

If you want to do it deeply and handle also other cases then simple maps AND preserve functions (unlike the json solution) then use this simple function:

_toDartSimpleObject(thing) {
 if (thing is js.JsArray) {
  List res = new List();
  js.JsArray a = thing as js.JsArray;
  a.forEach((otherthing) {
    res.add(_toDartSimpleObject(otherthing));
  });
  return res;

 } else if (thing is js.JsObject) {
  Map res = new Map();
  js.JsObject o = thing as js.JsObject;
  Iterable<String> k = js.context['Object'].callMethod('keys', [o]);
   k.forEach((String k) {
   res[k] = _toDartSimpleObject(o[k]);
  });
  return res;

 } else {
  return thing;
 }
}

这篇关于如何将JavaScript对象转换为Dart Map?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆