如何处理空的 JSON 对象?
Posted
技术标签:
【中文标题】如何处理空的 JSON 对象?【英文标题】:How to deal with empty JSON object? 【发布时间】:2022-01-19 12:45:34 【问题描述】:我正在使用在应用启动时返回空列表的 API。它在特定行中显示以下错误: productList: List<ProductList>.from(json["ProductList"].map((x) => ProductList.fromJson(x))),
我该如何处理这个错误?我看到了一些解决方案,他们提供了一个空列表 []。如果你只是告诉我我应该在这里使用什么语法来处理这种错误......
对于这个错误,我的应用有时会无限加载。
我的模型课
import 'package:meta/meta.dart';
import 'dart:convert';
MyCartItemListModel myCartItemListModelFromJson(String str) =>
MyCartItemListModel.fromJson(json.decode(str));
String myCartItemListModelToJson(MyCartItemListModel data) =>
json.encode(data.toJson());
class MyCartItemListModel
MyCartItemListModel(
required this.status,
required this.message,
required this.userId,
required this.cookieVal,
required this.totalItem,
required this.cartProductTotal,
required this.shippingCharge,
required this.cartTotal,
required this.productList,
);
int status;
String message;
String userId;
String cookieVal;
String totalItem;
String cartProductTotal;
String shippingCharge;
String cartTotal;
List<ProductList> productList;
factory MyCartItemListModel.fromJson(Map<String, dynamic> json) =>
MyCartItemListModel(
status: json["Status"],
message: json["Message"],
userId: json["UserId"] ??"",
cookieVal: json["CookieVal"],
totalItem: json["TotalItem"],
cartProductTotal: json["CartProductTotal"],
shippingCharge: json["ShippingCharge"],
cartTotal: json["CartTotal"],
productList: List<ProductList>.from(json["ProductList"].map((x) => ProductList.fromJson(x))),
);
Map<String, dynamic> toJson() =>
"Status": status,
"Message": message,
"UserId": userId,
"CookieVal": cookieVal,
"TotalItem": totalItem,
"CartProductTotal": cartProductTotal,
"ShippingCharge": shippingCharge,
"CartTotal": cartTotal,
"ProductList": List<dynamic>.from(productList.map((x) => x.toJson())),
;
class ProductList
ProductList(
required this.cartId,
required this.productId,
required this.productName,
required this.productImage,
required this.productSize,
required this.productColor,
required this.productRate,
required this.quantity,
required this.productTotal,
);
String cartId;
String productId;
String productName;
String productImage;
String productSize;
String productColor;
String productRate;
String quantity;
String productTotal;
factory ProductList.fromJson(Map<String, dynamic> json) => ProductList(
cartId: json["CartId"],
productId: json["ProductId"],
productName: json["ProductName"],
productImage: json["ProductImage"],
productSize: json["ProductSize"],
productColor: json["ProductColor"],
productRate: json["ProductRate"],
quantity: json["Quantity"],
productTotal: json["ProductTotal"],
);
Map<String, dynamic> toJson() =>
"CartId": cartId,
"ProductId": productId,
"ProductName": productName,
"ProductImage": productImage,
"ProductSize": productSize,
"ProductColor": productColor,
"ProductRate": productRate,
"Quantity": quantity,
"ProductTotal": productTotal,
;
我的 json 响应:
"Status": 1,
"Message": "",
"UserId": "2",
"CookieVal": "",
"TotalItem": "4",
"CartProductTotal": "1767",
"ShippingCharge": "50",
"CartTotal": "1817",
"ProductList": [
"CartId": "450",
"ProductId": "10622",
"ProductName": " Kids Baby Leggings Pink",
"ProductImage": "https://sleepkart.co.in/productimage/zb9diak47ocm0q957itf_1.jpg",
"ProductSize": "L",
"ProductColor": "#fdc291",
"ProductRate": "190",
"Quantity": "1",
"ProductTotal": "190"
,
"CartId": "449",
"ProductId": "10623",
"ProductName": "Kids Baby Leggings Green",
"ProductImage": "https://sleepkart.co.in/productimage/ogr137q1kjr9fiqwdipd_1.jpg",
"ProductSize": "L",
"ProductColor": "#42d19a",
"ProductRate": "193",
"Quantity": "1",
"ProductTotal": "193"
,
"CartId": "438",
"ProductId": "10661",
"ProductName": "Night Suit for Women",
"ProductImage": "https://sleepkart.co.in/productimage/4jcrpnqw655vg7yoyvun_1.jpg",
"ProductSize": "L",
"ProductColor": "#f2be02",
"ProductRate": "975",
"Quantity": "1",
"ProductTotal": "975"
,
"CartId": "437",
"ProductId": "10575",
"ProductName": "Men's Navy Blue Bermuda",
"ProductImage": "https://sleepkart.co.in/productimage/zn8oqvspajuks9u1pre4_1.jpg",
"ProductSize": "FREE",
"ProductColor": "#0c1155",
"ProductRate": "409",
"Quantity": "1",
"ProductTotal": "409"
]
【问题讨论】:
你能提供这个api的示例json吗? 嘿,我只是添加我的 JSON 响应。当我的应用程序首先启动时,"ProductList"
是空的。这就是我猜错误的原因。如果你只是帮我处理这个错误
当你得到空列表时,ProductList:[]
作为 json 中的字段??
Yes , ProductList:[]
as field in json .. 最初当我的应用程序第一次启动或用户删除所有项目表单列表(购物车)然后我的 ProductList:[]
empty ,错误发生有时这个错误不打扰。但我想处理这个错误。
我理解你的问题,几天前发生在我身上,这是来自后端的错误,有时一个字段不是来自后端的 json 响应,所以检查一下
【参考方案1】:
你可以这样检查:
productList: json["ProductList"] == null ? [] :
List<ProductList>.from(json["ProductList"].map((x)
=>ProductList.fromJson(x)
【讨论】:
我认为这确实会有所帮助!以上是关于如何处理空的 JSON 对象?的主要内容,如果未能解决你的问题,请参考以下文章
如何处理空的“DataFrame”:没有数字数据来绘制错误以在图表上获取字符串
java.lang.NullPointerException - 如何处理空指针异常
java.lang.NullPointerException- 如何处理空指针异常