我试图在JsonOject库的帮助下解析一些JSON.我收到以下异常:
Exception: type 'List' is not a subtype of type 'Map' of 'value'.
Run Code Online (Sandbox Code Playgroud)
我的代码:
class MyList extends JsonObject implements List {
MyList();
factory MyList.fromString(String jsonString) {
return new JsonObject.fromJsonString(jsonString, new MyList());
}
}
class MyResult extends JsonObject {
num Dis;
int Flag;
MyProduct Obj;
}
class MyProduct extends JsonObject {
int ID;
String Title;
}
Run Code Online (Sandbox Code Playgroud)
我称之为:
var testJson = """
[{"Dis":1111.1,"Flag":0,"Obj":{"ID":1,"Title":"Volvo 140"}},
{"Dis":2222.2,"Flag":0,"Obj":{"ID":2,"Title":"Volvo 240"}}]
""";
MyList list = new MyList.fromString(testJson);
//I want to be able to do something like this. …Run Code Online (Sandbox Code Playgroud) 这个问题与这篇文章相关。
我尝试了以下代码:
import 'dart:convert';
/*server side Post class */
class Post {
int post_id;
String title;
String description;
DateTime posted_at;
DateTime last_edited;
String user;
String editor;
int up_votes;
int down_votes;
int total_votes;
String links_to;
List<String> tags = new List();
Post.fromSQL(List sql_post) {
//initialization code, unrelated.
}
Map toJson(){
Map fromObject = {
'post_id' : post_id,
'title' : title,
'description' : description,
'posted_at' : posted_at,
'last_edited' : last_edited,
'user' : user,
'editor' : editor,
'up_votes' : up_votes,
'dwon_votes' : down_votes, …Run Code Online (Sandbox Code Playgroud)