Skip to content

Instantly share code, notes, and snippets.

@Quingsley
Created August 11, 2022 15:16
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 Quingsley/656b2c9e937320517ac9c00a463f258c to your computer and use it in GitHub Desktop.
Save Quingsley/656b2c9e937320517ac9c00a463f258c to your computer and use it in GitHub Desktop.
working-with-json-data

working-with-json-data

Created with <3 with dartpad.dev.

import 'dart:convert';
void main() {
// this represents some response data we get from the network
final jsonData = '{ "name": "Pizza da Mario", "cuisine": "Italian" }';
// 2. decode the json
final parsedJson = jsonDecode(jsonData);
// 3. print the type and value
print('${parsedJson.runtimeType} : $parsedJson');
// Creating a Map with Name and ids of students
Map <String,int> mp={'Ankur':1,'Arnav':002,'Shivam':003};
print('Map :$mp');
var person = '{"url":"http://blah.jpg","id":21}';
var decodePerson = jsonDecode(person);
print('$decodePerson');
var imageModel = ImageModel.fromJson(decodePerson);
print(imageModel.url);
}
//model classes of json
class ImageModel{
int id = 0;
String url = '';
ImageModel(this.id,this.url);
//named constructor
ImageModel.fromJson(parsedJson){
id = parsedJson['id'];
url = parsedJson['url'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment