Skip to content

Instantly share code, notes, and snippets.

@claudiodangelis
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save claudiodangelis/be4e6acba03fd58e634f to your computer and use it in GitHub Desktop.
Save claudiodangelis/be4e6acba03fd58e634f to your computer and use it in GitHub Desktop.
{
"uno": "one",
"two": "due",
"data": {
"x":"y"
}
}
import 'dart:io' show File;
import 'dart:async' show Stream;
import 'dart:convert' show UTF8, JSON;
void main() {
// questo sara il tuo json
Map tuoJson;
// crei un nuovo oggetto file
File file = new File('data.json');
// il metodo openRead() legge il file in modo asincrono, restituisce uno stream
//
Stream stream = file.openRead();
// openRead legge il file byte per byte quindi abbiamo bisogno di un "transformer"
// che converte al volo i dati che riceve
// in questo caso il converte in caratteri stringa (UTF8.decoder) e li passa
// a listen, che "ascolta" i dati che gli arrivano dallo stream
stream.transform(UTF8.decoder).listen((data) {
// converte "data" che è una stringa che rappresenta un JSON in un oggetto
// dart nativo, una mappa
tuoJson = JSON.decode(data);
// stampa tutto il json
print(tuoJson);
// stampa il valore di ["data"]["x"], ovvero y
print(tuoJson["data"]["x"]);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment