【问题标题】:What is equivalent in Flutter for JSON parsing?Flutter 中 JSON 解析的等价物是什么?
【发布时间】:2020-08-25 01:59:36
【问题描述】:

Flutter 中有没有 G-SON/Jackson/Mos-hi/ 等价物?

【问题讨论】:

  • 你的用例是什么,可以使用jsonDecode/jsonEncode

标签: json flutter


【解决方案1】:

使用 dart:convert 包的 json。这是一个例子:

import 'dart:convert' as JSON;

main() {
  final json = '{"a": "b"}';
  final decoded = JSON.jsonDecode(json);
}

查看官方文档here

在我的最新项目中,我使用json_serializable,它对我很有效

【讨论】:

    【解决方案2】:

    在 Dart 中,您不必导入 Jackson、Gson 等包。只需使用json.encode() 编码和json.decode() 解码。或者正如其他人提到的那样使用jsonDecode / jsonEncode。所有可通过 Dart 的import 'dart:convert' 获得。

    例如:

    import 'dart:convert';
    //....
    String str = "{'name': 'Asanka'}"l
    
    try {
      Map<String, dynamic> map = json.decode(str);
      print(map["name"]);
    
      String backToStr = json.encode(map);
      print(backToStr);
    } catch (e) {
      print(e);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-03
      • 1970-01-01
      • 2012-02-26
      • 1970-01-01
      • 1970-01-01
      • 2014-05-08
      • 2014-06-12
      • 1970-01-01
      相关资源
      最近更新 更多