我有一个生成json列表的类,这是我的类
class Tool{
String Name;
bool selection;
String englishName;
Map<String, dynamic> toJson() {
return {
'Name': persianName,
'selection': selection,
'englishName': englishName
};
}
}
List<Tool> tools=new List();
setTool(tool){
tools.add(tool);
}
toolsLength(){
return tools.length;
}
updatetool(index,goal){
tools[index]=goal;
}
getTool(index){
return tools[index];
}
getAllTools(){
return tools;
}
这是一个dio库,可以将我的列表发送到服务器,一切正常,但是我的数组进入了双引号。换句话说,我的列表是字符串,如何在json数组周围获取双引号。如果假设我的json数组急需
"tools": [{"name":"jack","selection" : false " ,"englishName":"jock"}]
结果是:
"tools": "[{"name":"jack","selection" : false " ,"englishName":"jock"}]"
如何解决,这是我要发送的课程
FormData formData = new FormData.from({
"tools":jsonEncode(getAllTools().map((e) => e.toJson()).toList()) ,
});
response = await
dio.post("${strings.baseurl}/analyze/$username/$teacher", data:
formData);
print("----------> response is :"+response.toString());
答案 0 :(得分:0)
修改
您可以将以下代码粘贴到DarPad https://dartpad.dartlang.org/
以下演示展示了将Json字符串转换为工具列表,再次将您的工具列表转换为JSON字符串,并使用map.toJson
var yourresult = toolList.map((e) => e.toJson()).toList();
您可以在图片中看到结果
使用FormData.from需要知道您的api正确的JSON字符串。
请使用Postman测试您的Web api,如果成功,您将知道正确的格式字符串。
在您的代码中
FormData formData = new FormData.from({
"tools":jsonEncode(getAllTools().map((e) => e.toJson()).toList()) ,
});
如果您想做
FormData formData = new FormData.from({
"tools":'[{"name":"jack","selection" : false ,"englishName":"jock"}, {"name":"jack2","selection" : false ,"englishName":"jock2"}]' ,
});
因此您可以首先获取json字符串并将其放入FormData
var toolsJson = toolsToJson(toolList);
FormData formData = new FormData.from({
"tools":toolsJson ,
});
完整的演示代码,您可以粘贴到DartPad以查看字符串和列表的转换
import 'dart:async';
import 'dart:io';
import 'dart:core';
import 'dart:convert';
class Tools {
String name;
bool selection;
String englishName;
Tools({
this.name,
this.selection,
this.englishName,
});
factory Tools.fromJson(Map<String, dynamic> json) => new Tools(
name: json["name"],
selection: json["selection"],
englishName: json["englishName"],
);
Map<String, dynamic> toJson() => {
"name": name,
"selection": selection,
"englishName": englishName,
};
}
main() {
List<Tools> toolsFromJson(String str) => new List<Tools>.from(json.decode(str).map((x) => Tools.fromJson(x)));
String toolsToJson(List<Tools> data) => json.encode(new List<dynamic>.from(data.map((x) => x.toJson())));
var toolsStr = '[{"name":"jack","selection" : false ,"englishName":"jock"}, {"name":"jack2","selection" : false ,"englishName":"jock2"}]';
var toolList = toolsFromJson(toolsStr);
var toolsJson = toolsToJson(toolList);
print("toolsJson ${toolsJson} \n");
var toolsmap = toolList[0].toJson();
print("toolsmap ${toolsmap.toString()}\n");
var yourresult = toolList.map((e) => e.toJson()).toList();
print(yourresult.toString());
}
您可以将JSON字符串粘贴到https://app.quicktype.io/,您将获得正确的Dart类
示例的正确JSON字符串。在false关键字中,后跟一个“会导致JSON字符串解析错误。
[
{"name":"jack",
"selection" : false ,
"englishName":"jock"
}
]
代码以解析JSON字符串并编码为List,使用toJson将转换为您需要以dio形式发送的JSON字符串
// To parse this JSON data, do
//
// final tools = toolsFromJson(jsonString);
import 'dart:convert';
List<Tools> toolsFromJson(String str) => new List<Tools>.from(json.decode(str).map((x) => Tools.fromJson(x)));
String toolsToJson(List<Tools> data) => json.encode(new List<dynamic>.from(data.map((x) => x.toJson())));
class Tools {
String name;
bool selection;
String englishName;
Tools({
this.name,
this.selection,
this.englishName,
});
factory Tools.fromJson(Map<String, dynamic> json) => new Tools(
name: json["name"],
selection: json["selection"],
englishName: json["englishName"],
);
Map<String, dynamic> toJson() => {
"name": name,
"selection": selection,
"englishName": englishName,
};
}