如果您想上传data 对象,排序答案是将其转换为如下字符串:
var jsonString = json.encode(data);
// upload jsonString as body to your server.
print("Json String is " + jsonString);
Map headers = {
'Content-type' : 'application/json',
};
http.post("www.server.com/myPostEndpoint", body: jsonString, headers: headers);
就像在 cmets 中讨论的那样,您可能需要将整个内容包装在 data 属性中。你可以试试这个:
var payload = {
"data": {
"email": "qwe@qwe.com",
"password": "123",
"first_name": "qweqwe",
"billing": {
"email": "qwe@qwe.com",
"phone": "321654978",
},
"shipping": {
"first_name": "qweqwe",
}
}
};
var jsonString = json.encode(payload); // upload jsonString as body to your server. print("Json String is " + jsonString);
Map headers = { 'Content-type': 'application/json', };
http.post("www.server.com/myPostEndpoint", body: jsonString, headers: headers);
tl;dr
创建模型总是一个好主意。
所以这可能是你的User 模型:
class User {
final String email;
final String password;
final String firstName;
final Map<String, String> billing;
final Map<String, String> shipping;
User({this.email, this.password, this.firstName, this.billing, this.shipping});
static User fromJson(dynamic json) {
return User(
email: json["email"],
password: json["password"],
firstName: json["first_name"],
billing: json["billing"],
shipping: json["shipping"],
);
}
}
它有一个静态构造函数,可以方便地从一段解码的 json 中创建这个 User 的实例,就像你拥有的那样。
现在您可以继续在您的代码中使用此模型,如下所示:
var data = {
"email": "qwe@qwe.com",
"password": "123",
"first_name": "qweqwe",
"billing": {
"email": "qwe@qwe.com",
"phone": "321654978",
},
"shipping": {
"first_name": "qweqwe",
}
};
var user = User.fromJson(data);
print("Users Email is ${user.billing["email"]}");
同时为Billing 和Shipping 创建模型会更简洁。
这是一个完整的示例,其中还包括从解析的模型生成 Json 的 toJson() 方法:
class User {
final String email;
final String password;
final String firstName;
final Billing billing;
final Shipping shipping;
User({this.email, this.password, this.firstName, this.billing, this.shipping});
static User fromJson(dynamic json) {
return User(
email: json["email"],
password: json["password"],
firstName: json["first_name"],
billing: Billing.fromJson(json["billing"]),
shipping: Shipping.fromJson(json["shipping"]),
);
}
Map<String, dynamic> toJson() => {'email': email, 'password': password, 'first_name': firstName, 'billing': billing.toJson(), 'shipping': shipping.toJson()};
}
class Billing {
final String email;
final String phone;
Billing({this.email, this.phone});
static Billing fromJson(dynamic json) {
return Billing(email: json["email"], phone: json["phone"]);
}
Map<String, dynamic> toJson() => {'email': email, 'phone': phone};
}
class Shipping {
final String name;
Shipping({
this.name,
});
static Shipping fromJson(dynamic json) {
return Shipping(
name: json["first_name"],
);
}
Map<String, dynamic> toJson() => {'name': name};
}
你可以这样使用它:
var data = {
"email": "qwe@qwe.com",
"password": "123",
"first_name": "qweqwe",
"billing": {
"email": "qwe@qwe.com",
"phone": "321654978",
},
"shipping": {
"first_name": "qweqwe",
}
};
var user = User.fromJson(data);
print("Users Email is ${user.billing.email}");
print("Shipping name is ${user.shipping.name}");
var jsonString = json.encode(user.toJson()); // upload this as body to your server.
print("Json String is " + jsonString);