如何在Dart中处理JSON [英] How to handle JSON in Dart

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

问题描述

我不知道Dart如何处理JSON?具体来说:


  1. 我可以存取JSON物件中的项目,以及何时,如何?

  2. 我将Darts数据结构(如Set和Maps)转换成JSON?

  3. 我可以创建一个新的JSON,只能通过调用JSON.parse?


你可能会发现这个帖子很有趣:
http://www.grobmeier.de/dart-creating-a-dynamic-list-with-dart-php-and-json-20112011.html



您需要使用JSON包(将json添加到pubspec.yaml):

  import'package:json / json。镖'; 

以下是相应的规格:
https://api.dartlang.org/docs/channels/stable/latest/json.html p>

您的问题:


  1. 您可以使用:List result = JSON.parse jsonData);

  2. 使用stringify可以转换为例如一个Map为JSON

  3. 对不起,不确定这个问题。你可以做:JSON.parse('{key:value}'));

您可能需要使用parse从JSON创建一个Map,然后添加您的项目,然后调用stringify


I wonder how does Dart handle JSON? More specifically:

  1. Can I access item in a JSON object and when, how?
  2. Can I convert Darts data structures like Set and Maps into JSON?
  3. Can I create a new JSON, only by calling JSON.parse?
  4. How can I add new items into a JSON?

解决方案

You might find this post of mine interesting: http://www.grobmeier.de/dart-creating-a-dynamic-list-with-dart-php-and-json-20112011.html

You need to use the JSON package (add json to pubspec.yaml):

import 'package:json/json.dart';

Here is the according spec: https://api.dartlang.org/docs/channels/stable/latest/json.html

To your questions:

  1. You can use: List result = JSON.parse( jsonData );
  2. With stringify you can turn for example a Map to JSON
  3. I am sorry, not sure on this question. You could do: JSON.parse('{key:"value"}')); or something like that
  4. You probably need to create a Map out of your JSON with parse, then add your item, and then call stringify

这篇关于如何在Dart中处理JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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