Last active
August 29, 2015 14:01
-
-
Save claudiodangelis/be4e6acba03fd58e634f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"uno": "one", | |
"two": "due", | |
"data": { | |
"x":"y" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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