我正在尝试解析json响应,但它返回null。这是远程示例json响应:
count 20
next null
previous null
results
0
id 1
name "some name"
name_bn "another name"
price 88
price_bn "৮৮"
old_price false
old_price_val null
old_price_val_bn null
image
0
image_mobile_feature_list "https://ex.com/m…fe708439802736d8ea7.webp"
image_mobile_product_details "https://ex.com/m…e542aaf37cb52c44523.webp"
slug "example"
short_description_opt false
short_description "<ul class=\"\" style=\"m…avoid;\"><br></li></ul>"
short_description_bn ""
description_opt true
description "<ul class=\"\" style=\"m…avoid;\"><br></li></ul>"
description_bn "<ul class=\"\" style=\"m…avoid;\"><br></li></ul>"
max_item 1
size false
size_list []
color false
color_list []
unit false
unit_en null
unit_bn null
ranking 3
//next object
产品型号:
class ProductModel {
final int id;
final String name;
final String nameBn;
final String price;
final String priceBn;
final bool oldPrice;
final String oldPriceVal;
final String oldPriceValBn;
final List<ImageBunny> image;
final String slug;
final String shortDescriptionOpt;
final String shortDescription;
final String shortDescriptionBn;
final int maxItem;
final bool size;
final List<String> sizeList;
final bool color;
final List<String> colorList;
final bool unit;
final String unitBn;
final String unitEn;
final int ranking;
ProductModel(
{this.id,
this.name,
this.nameBn,
this.price,
this.priceBn,
this.oldPrice,
this.oldPriceVal,
this.oldPriceValBn,
this.image,
this.slug,
this.shortDescriptionOpt,
this.shortDescription,
this.shortDescriptionBn,
this.maxItem,
this.color,
this.colorList,
this.ranking,
this.size,
this.sizeList,
this.unit,
this.unitBn,
this.unitEn
});
}
图像模型:
class ImageBunny {
final String imageMobileFeatureList;
final String imageMobileProductDetails;
ImageBunny({
this.imageMobileFeatureList,
this.imageMobileProductDetails,
});
}
以及响应解析方法:
Future<List<ProductModel>> _parseProductsFromResponse() async {
List<ProductModel> productsList = <ProductModel>[];
var response = await http.get('https://url');
var dataFromResponse = json.decode(response.body);
dataFromResponse['results'].forEach(
(newProduct) {
print(newProduct); // printing only 1st object
//parse the product's images
List<ImageBunny> imagesOfProductList = [];
newProduct["image"].forEach(
(newImage) {
imagesOfProductList.add(
ImageBunny(
imageMobileFeatureList: newImage["imageMobileFeatureList"],
imageMobileProductDetails: newImage["imageMobileProductDetails"],
),
);
},
);
// parse Size List
List<String> sizeList = [];
newProduct["size_list"].forEach((value) {
sizeList.add(value);
});
// parse Color List
List<String> colorList = [];
newProduct["color_list"].forEach((value) {
var color = value.toString();
var colorVal = color.substring(1, color.length);
colorList.add(colorVal);
});
//parse new product's details
ProductModel product = ProductModel(
id: newProduct["id"],
name: newProduct["name"].toString(),
nameBn: newProduct["name_bn"].toString(),
price: newProduct["price"].toString(),
priceBn: newProduct["price_bn"].toString(),
oldPrice: newProduct["old_price"],
oldPriceVal: newProduct["old_price_val"] != null? newProduct["old_price_val"].toString(): "",
oldPriceValBn: newProduct["old_price_val_bn"] != null? newProduct["old_price_val_bn"].toString(): "",
slug: newProduct["slug"].toString(),
shortDescriptionOpt: newProduct["short_description_opt"],
shortDescription: newProduct["short_description"].toString(),
shortDescriptionBn: newProduct["short_description_bn"].toString(),
maxItem: newProduct["max_item"],
size: newProduct["size"],
sizeList: sizeList,
color: newProduct["color"],
colorList: colorList,
unit: newProduct["unit"],
unitBn: newProduct["unit_bn"] != null? newProduct["unit_bn"].toString() : "",
unitEn: newProduct["unit_en"] != null? newProduct["unit_en"].toString() : "",
ranking: newProduct["ranking"],
image: imagesOfProductList,
);
print(product); // not printing anything
print("hit"); // not printing anything
productsList.add(product);
},
);
return productsList;
}
}