在 Dart 中解析 JSON 的最佳方法 [英] The best way to parse a JSON in Dart

查看:168
本文介绍了在 Dart 中解析 JSON 的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 URL 加载 json 文件并在 Dart 中对其进行解析.所以我在谷歌搜索时按照一些链接的建议尝试了以下代码:

 HttpRequest.getString("hellknight2.js").then((响应){var model = new JSON.parse(response);});

然而,它似乎不再适用于 Dart SDK 版本 0.4.3.5_r20602.当前将 Json 文件映射到 Dart 中的对象的最佳方法是什么?

解决方案

只需使用 json>dart:convert 包.这是一个例子:

import 'dart:convert';主要的() {最后 myJsonAsString = '{"a": 1, "b": "c"}';最终解码 = json.decode(myJsonAsString);....}

有关详细信息,请参阅解析 JSON.>

I'm trying to load a json file from a URL and parse it within Dart. So I had tried the following code as suggested from some links when I google for it:

   HttpRequest.getString("hellknight2.js").then((response)
   {
     var model = new JSON.parse(response);
   });

However, it seems to not work anymore on Dart SDK version 0.4.3.5_r20602. What is the current best way to get a Json file mapped to an object in Dart?

解决方案

Simply use json of the dart:convert package. Here is an example :

import 'dart:convert';

main() {
  final myJsonAsString = '{"a": 1, "b": "c"}';
  final decoded = json.decode(myJsonAsString);
  ....
}

See Parsing JSON for more details.

这篇关于在 Dart 中解析 JSON 的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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