How to handle JSON in Dart

Like Christian, there's also a similar post on my dartwatch blog which might be useful.


You can use the json property provided by the dart:convert library.

import 'dart:convert' show json;

main() {
  var encoded = json.encode([1, 2, { "a": null }]);
  var decoded = json.decode('["foo", { "bar": 499 }]');
}

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

Tags:

Json

Dart