我正在try 将我的JSON文件映射到一个类对象,然后根据新收到的JSON更新卡.
我的JSON struct 是这样的
{
"$class": "FirstCard",
"id": "1",
"description": "I am card number one",
"Role": "attack",
"score": 0,
"tag": [
"string"
],................}
我的班级是这样的:
class CardInfo {
//Constructor
String id;
String description;
String role;
int score;
}
如何将JSON文件中的值映射到从CardInfo类创建的对象的字段中?
Update个
下面的试验打印空值为ci.description,这是否意味着从未创建过该对象?
const jsonCodec = const JsonCodec
_loadData() async {
var url = 'myJsonURL';
var httpClient = createHttpClient();
var response =await httpClient.get(url);
print ("response" + response.body);
Map cardInfo = jsonCodec.decode(response.body);
var ci = new CardInfo.fromJson(cardInfo);
print (ci.description); //prints null
}
Update2个
打印cardInfo提供以下信息:
{$class:FirstCard,id:1,描述:我是第一张卡,.}
请注意,它类似于原始的JSON,但字符串值上没有双引号.