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