Flutter: Handle JSON Deep Dive

Started
Photo by Dayne Topkin on Unsplash
getData() async{
http.Response response = await http.get("url");
var result = json.decode(response.body);
print(result);
}
{
"id" : 1,
"first_name" : "Varun",
"last_name" : "Verma",
"email" : "rajputvarun591@gmail.com",
"address" : "Khatauli UttarPradesh India 251201",
"phone" : "+910000000000",
"height" : 157.58,
"is_active" : true
}
class MyProfile{
int id;
String firstName;
String lastName;
String email;
String address;
String phone;
double height;
bool isActive;
}
MyProfile.fromJsonResp(Map<String, dynamic> json)
: id = json["id"],
firstName = json["first_name"],
lastName = json["last_name"],
email = json["email"],
address = json["address"],
phone = json["phone"],
height = json["height"],
isActive = json["is_active"];
Future getData() async{
http.Response response = await http.get("url");
var result = json.decode(response.body);
MyProfile myProfile = MyProfile.fromJsonResp(result);
print(myProfile.toString());
}
Photo by Clark Tibbs on Unsplash
{
"id" : 1,
"first_name" : "Varun",
"last_name" : "Verma",
"email" : "rajputvarun591@gmail.com",
"address" : {
"district" : "Meerut",
"city" : "Khatauli",
"country" : "India",
"pin" : 251201
},

"phone" : "+910000000000",
"height" : 157.58,
"is_active" : true
}
class Address {
String district;
String city;
String country;
int pin;

Address.fromJsonResp(Map<String, dynamic> json)
: district = json["district"],
city = json["city"],
country = json["country"],
pin = json["pin"];
}
class MyProfile{
int id;
String firstName;
String lastName;
String email;
Address address;
String phone;
double height;
bool isActive;

MyProfile.fromJsonResp(Map<String, dynamic> json)
: id = json["id"],
firstName = json["first_name"],
lastName = json["last_name"],
email = json["email"],
address = Address.fromJsonResp(json["address"]),
phone = json["phone"],
height = json["height"],
isActive = json["is_active"];
}
{
"id" : 1,
"first_name" : "Varun",
"last_name" : "Verma",
"email" : "rajputvarun591@gmail.com",
"address" : {
"district" : "Meerut",
"city" : "Khatauli",
"country" : "India",
"pin" : 251201
},
"phone" : ["+910000000000", "+910000123456", "+910001234567", "+910012345678", "+910123456789"],
"height" : 157.58,
"is_active" : true
}
1. class MyProfile{
2. int id;
3. String firstName;
4. String lastName;
5. String email;
6. Address address;
7. List<String> phone;
8. double height;
9. bool isActive;

10.MyProfile.fromJsonResp(Map<String, dynamic> json)
11. : id = json["id"],
12. firstName = json["first_name"],
13. lastName = json["last_name"],
14. email = json["email"],
15. address = Address.fromJsonResp(json["address"]),
16. phone = (json["phone"] as List<dynamic>).map((e) => e.toString()).toList(),
17. height = json["height"],
18. isActive = json["is_active"];
19.}
{
"id" : 1,
"first_name" : "Varun",
"last_name" : "Verma",
"email" : "rajputvarun591@gmail.com",
"address" : {
"district" : "Meerut",
"city" : "Khatauli",
"country" : "India",
"pin" : 251201
},
"phone" : ["+910000000000", "+910000123456", "+910001234567"],
"height" : 157.58,
"is_active" : true,
"experiences" : [
{
"years" : 4,
"company" : "ABC Pvt Ltd",
"company_id" : 101,
"started_on" : "2020/02/02",
"ended_on" : "2020/02/02",
},
{
"years" : 5,
"company" : "DEF Pvt Ltd",
"company_id" : 102,
"started_on" : "2020/02/02",
"ended_on" : "2020/02/02",
},
{
"years" : 8,
"company" : "GHI Pvt Ltd",
"company_id" : 103,
"started_on" : "2020/02/02",
"ended_on" : "2020/02/02",
}
]

}
class Experiences {
int years;
String company;
String companyId;
String startedOn;
String endedOn;

Experiences.fromJsonResp(Map<String, dynamic> json)
: years = json["years"],
company = json["company"],
companyId = json["company_id"],
startedOn = json["started_on"],
endedOn = json["ended_on"];
}
class MyProfile {
int id;
String firstName;
String lastName;
String email;
Address address;
List<String> phone;
double height;
bool isActive;
List<Experiences> experiences;

MyProfile.fromJsonResp(Map<String, dynamic> json)
: id = json["id"],
firstName = json["first_name"],
lastName = json["last_name"],
email = json["email"],
address = Address.fromJsonResp(json["address"]),
phone = (json["phone"] as List<dynamic>).map((e) => e.toString()).toList(),
height = json["height"],
isActive = json["is_active"],
experiences = (json["experiences"] as List<dynamic>).map((e) => Experiences.fromJsonResp(e)).toList();
}
{
"status": "success",
"msg": "fetched",
"data": {
"text": "Poor",
"alert": "Poor air quality index in this location. It may cause breathing discomfort to people on prolonged exposure, and discomfort to people with heart disease.",
"color": "#FF9A00;",
"value": 300,
"index": 4,
"updated": "Thu Dec 03 06:15:00 +0000 2020",
"temp": "23",
"content": null,
"country": "India",
"image": "https://api.com/images/icons/50d.png",
"coordinates": {
"latitude": 28.704059,
"longitude": 77.10249
},
"source": {
"name": "CPCB, Mundka, Delhi DPCC",
"coordinates": {
"latitude": 28.684678,
"longitude": 77.076574
}
},
"accuracy": "3.32 Km",
"dominating": "PM2.5",
"aqiParams": [
{
"name": "NO2",
"value": "35.7 µg/m³, AQI 44",
"aqi": 44,
"color": "#bbcf4c;",
"text": "Satisfactory",
"updated": "Thu Dec 03 06:15:00 +0000 2020"
},
{
"name": "PM2.5",
"value": "186.0 µg/m³, AQI 300",
"aqi": 300,
"color": "#A52A2A;",
"text": "Severe",
"updated": "Thu Dec 03 06:15:00 +0000 2020"
},
{
"name": "SO2",
"value": "22.4 µg/m³, AQI 22",
"aqi": 22,
"color": "#79bc6a;",
"text": "Good",
"updated": "Thu Dec 03 06:15:00 +0000 2020"
},
{
"name": "PM10",
"value": "280.0 µg/m³, AQI 230",
"aqi": 230,
"color": "#FFCF00;",
"text": "Moderate",
"updated": "Thu Dec 03 06:15:00 +0000 2020"
},
{
"name": "CO",
"value": "1200.0 µg/m³, AQI 60",
"aqi": 60,
"color": "#A52A2A;",
"text": "Severe",
"updated": "Thu Dec 03 06:15:00 +0000 2020"
},
{
"name": "Humidity",
"value": "38.0 %",
"aqi": null,
"color": null,
"text": null,
"updated": null
},
{
"name": "Barometric Pressure",
"value": "1015.0 hPa",
"aqi": null,
"color": null,
"text": null,
"updated": null
},
{
"name": "Wind Speed",
"value": " 3.36 m/s",
"aqi": null,
"color": null,
"text": null,
"updated": null
},
{
"name": "Wind Direction",
"value": "270.0 degrees",
"aqi": null,
"color": null,
"text": null,
"updated": null
}
]
}
}
Outer JSON Object
http.Response response = await http.get("url");
var result = json.decode(response.body);
if(result["status"] == "success") {
var data = result["data"];
print(data);
}
  1. Whenever you not sure about the value of an parameter(some parameters having “null ”value) then you may confuse to declare Data Type of variable in Model class . then simply take var instead of any data type. consider below.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store