Flutter 颤振json解析失败,无任何错误
我试图解析json响应,但它返回null。以下是远程示例json响应:Flutter 颤振json解析失败,无任何错误,flutter,Flutter,我试图解析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 imag
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
以及响应解析的方法:
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;
}
}
Future\u parseProductsFromResponse()异步{
列表产品列表=[];
var response=wait http.get('https://url');
var dataFromResponse=json.decode(response.body);
dataFromResponse['results'].forEach(
(新产品){
打印(新产品);//仅打印第一个对象
//解析产品的图像
List imagesOfProductList=[];
新产品[“图像”]。forEach(
(新图片){
imagesOfProductList.add(
成像兔(
imageMobileFeatureList:newImage[“imageMobileFeatureList”],
imageMobileProductDetails:newImage[“imageMobileProductDetails”],
),
);
},
);
//解析大小列表
列表大小列表=[];
新产品[“大小列表”].forEach((值){
增加(价值);
});
//解析颜色列表
列表颜色列表=[];
新产品[“颜色列表”].forEach((值){
var color=value.toString();
var colorVal=color.substring(1,color.length);
colorList.add(colorVal);
});
//解析新产品的详细信息
ProductModel产品=ProductModel(
id:newProduct[“id”],
名称:新产品[“名称”]。toString(),
nameBn:newProduct[“name_bn”].toString(),
价格:新产品[“价格”]。toString(),
priceBn:newProduct[“price_bn]”。toString(),
旧价格:新产品[“旧价格”],
oldPriceVal:newProduct[“old\u price\u val”]!=null?newProduct[“old\u price\u val”]。toString():“”,
oldPriceValBn:newProduct[“old\u price\u val\u bn”]!=null?newProduct[“old\u price\u val\u bn”]。toString():“”,
slug:newProduct[“slug”].toString(),
shortDescriptionOpt:newProduct[“short\u description\u opt”],
shortDescription:newProduct[“short_description”]。toString(),
shortDescriptionBn:newProduct[“short\u description\u bn]”。toString(),
maxItem:newProduct[“max_item”],
尺寸:新产品[“尺寸”],
sizeList:sizeList,
颜色:新产品[“颜色”],
颜色列表:颜色列表,
单位:新产品[“单位”],
unitBn:newProduct[“unit\u bn”]!=null?newProduct[“unit\u bn”]。toString():“”,
unitEn:newProduct[“unit_en”]!=null?newProduct[“unit_en”]。toString():“”,
排名:新产品[“排名”],
图像:imagesOfProductList,
);
打印(产品);//不打印任何东西
打印(“命中”);//不打印任何内容
产品列表。添加(产品);
},
);
退货产品清单;
}
}
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;
}
}