Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The best way to parse a JSON in Dart

Tags:

html

dart

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?

like image 544
Fernando Avatar asked Apr 07 '13 18:04

Fernando


People also ask

Which dart library is used for JSON parsing?

The dart:convert library provides support for JSON. Use HttpRequest to dynamically load data.

What does JSON decode do in dart?

jsonDecode function Null safetyParses the string and returns the resulting Json object. The optional reviver function is called once for each object or list property that has been parsed during decoding.


1 Answers

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.

like image 122
Alexandre Ardhuin Avatar answered Sep 27 '22 20:09

Alexandre Ardhuin