3

我试图在 JsonOject 库的帮助下解析一些 JSON。我收到以下异常:

 Exception: type 'List' is not a subtype of type 'Map' of 'value'.

我的代码:

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;
}

我这样称呼它:

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.
//print(list[0].Obj.Title);

我在这里做错了什么?

4

1 回答 1

6

我已经更新了我的JsonObject github repo来修复这个错误。我还添加了一个通过单元测试来重现这一点。

您需要运行pub update以获取最新版本的库。

以下代码现在可以工作:

import 'package:json_object/json_object.dart';

void main() {
  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.
  print(list[0].Obj.Title);  // <- now prints "Volvo 140"
}

class MyList extends JsonObject implements List { 
  MyList();

  factory MyList.fromString(String jsonString) {
    return new JsonObject.fromJsonString(jsonString, new MyList());
  }
}
于 2013-01-22T15:48:12.683 回答