parsing the json reponse

38 views
Skip to first unread message

Gaurav Swarankar

unread,
Jun 9, 2020, 3:05:23 AM6/9/20
to Flutter Development (flutter-dev)
i want to parse the json and setting the response into model class. but i stucked here. any help




var data = json.decode(response.body);
//var rest = data["NearbySalons_deals"];
//print(rest);

var rest = data['NearbySalons_deals'];
print(rest);

var offersjson = rest['Offers'] as List;
print(offersjson);


list = rest<NewTabDetailsModel>((json) => NewTabDetailsModel.fromJson(json));




My Model class ::





import 'package:artistry/main.dart';
import 'package:flutter/material.dart';

class NewTabDetailsModel {
int vendorID;
String vendorName;
String vendorDescription;
String contactNo;
String area;
String address;
double latitude;
double longitude;
int avgRating;
int ratingCount;
String logoURL;
List<String> images;
String categories;
String openTime;
String closeTime;
String createdAt;
String updatedAt;
List<Offers> offers;

NewTabDetailsModel(
{this.vendorID,
this.vendorName,
this.vendorDescription,
this.contactNo,
this.area,
this.address,
this.latitude,
this.longitude,
this.avgRating,
this.ratingCount,
this.logoURL,
this.images,
this.categories,
this.openTime,
this.closeTime,
this.createdAt,
this.updatedAt,
this.offers});

NewTabDetailsModel.fromJson(Map<String, dynamic> json) {
vendorID = json['VendorID'];
vendorName = json['VendorName'];
vendorDescription = json['VendorDescription'];
contactNo = json['ContactNo'];
area = json['Area'];
address = json['Address'];
latitude = json['Latitude'];
longitude = json['Longitude'];
avgRating = json['AvgRating'];
ratingCount = json['RatingCount'];
logoURL = json['LogoURL'];
images = json['Images'].cast<String>();
categories = json['Categories'];
openTime = json['OpenTime'];
closeTime = json['CloseTime'];
createdAt = json['CreatedAt'];
updatedAt = json['UpdatedAt'];
if (json['Offers'] != null) {
offers = new List<Offers>();
json['Offers'].forEach((v) {
offers.add(new Offers.fromJson(v));
});
}
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['VendorID'] = this.vendorID;
data['VendorName'] = this.vendorName;
data['VendorDescription'] = this.vendorDescription;
data['ContactNo'] = this.contactNo;
data['Area'] = this.area;
data['Address'] = this.address;
data['Latitude'] = this.latitude;
data['Longitude'] = this.longitude;
data['AvgRating'] = this.avgRating;
data['RatingCount'] = this.ratingCount;
data['LogoURL'] = this.logoURL;
data['Images'] = this.images;
data['Categories'] = this.categories;
data['OpenTime'] = this.openTime;
data['CloseTime'] = this.closeTime;
data['CreatedAt'] = this.createdAt;
data['UpdatedAt'] = this.updatedAt;
if (this.offers != null) {
data['Offers'] = this.offers.map((v) => v.toJson()).toList();
}
return data;
}
}

class Offers {
int offerID;
int vendorID;
int categoryID;
String offerTitle;
String description;
int offerPercentage;
String fromDateTime;
String toDateTime;
int actualPrice;
int offerPrice;
int status;
int qty;
String createdAt;
String updatedAt;

Offers(
{this.offerID,
this.vendorID,
this.categoryID,
this.offerTitle,
this.description,
this.offerPercentage,
this.fromDateTime,
this.toDateTime,
this.actualPrice,
this.offerPrice,
this.status,
this.qty,
this.createdAt,
this.updatedAt});

Offers.fromJson(Map<String, dynamic> json) {
offerID = json['OfferID'];
vendorID = json['VendorID'];
categoryID = json['CategoryID'];
offerTitle = json['OfferTitle'];
description = json['Description'];
offerPercentage = json['OfferPercentage'];
fromDateTime = json['FromDateTime'];
toDateTime = json['ToDateTime'];
actualPrice = json['ActualPrice'];
offerPrice = json['OfferPrice'];
status = json['Status'];
qty = json['Qty'];
createdAt = json['CreatedAt'];
updatedAt = json['UpdatedAt'];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['OfferID'] = this.offerID;
data['VendorID'] = this.vendorID;
data['CategoryID'] = this.categoryID;
data['OfferTitle'] = this.offerTitle;
data['Description'] = this.description;
data['OfferPercentage'] = this.offerPercentage;
data['FromDateTime'] = this.fromDateTime;
data['ToDateTime'] = this.toDateTime;
data['ActualPrice'] = this.actualPrice;
data['OfferPrice'] = this.offerPrice;
data['Status'] = this.status;
data['Qty'] = this.qty;
data['CreatedAt'] = this.createdAt;
data['UpdatedAt'] = this.updatedAt;
return data;
}
}

Suzuki Tomohiro

unread,
Jun 9, 2020, 7:05:41 AM6/9/20
to Gaurav Swarankar, Flutter Development (flutter-dev)
Any error message?

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/d46684c4-b549-4809-809d-8ddc231a0854o%40googlegroups.com.

Suzuki Tomohiro

unread,
Jun 9, 2020, 7:10:40 AM6/9/20
to Flutter Development (flutter-dev), Gaurav Swarankar
Good. Do you know which line of your code throw Th e exception?

Paste the stack trace as text.

On Tue, Jun 9, 2020 at 07:07 Gaurav Swarankar <gauravsw...@gmail.com> wrote:

Suzuki Tomohiro

unread,
Jun 11, 2020, 8:25:52 AM6/11/20
to Flutter Development (flutter-dev), Gaurav Swarankar
What is unclear about stacktrace?

On Thu, Jun 11, 2020 at 06:14 Gaurav Swarankar <gauravsw...@gmail.com> wrote:
How to use stack trace. I see most of the times but i don't know

Suzuki Tomohiro

unread,
Jun 11, 2020, 8:54:51 AM6/11/20
to Flutter Development (flutter-dev), Gaurav Swarankar
It gives the function call relationship when an exception happens. You can read it to identify why it happened.

On Thu, Jun 11, 2020 at 08:31 Gaurav Swarankar <gauravsw...@gmail.com> wrote:
How to use this? What is the use of this?

Gaurav Swarankar

unread,
Jun 12, 2020, 5:18:29 AM6/12/20
to Suzuki Tomohiro, Flutter Development (flutter-dev)
Got it. 
But how to use this?? Want to know the practical use

Suzuki Tomohiro

unread,
Jun 12, 2020, 6:51:17 AM6/12/20
to Gaurav Swarankar, Flutter Development (flutter-dev)
Read it and identify which line of your code caused the exception.
Reply all
Reply to author
Forward
0 new messages