【发布时间】:2017-12-24 15:35:46
【问题描述】:
我正在尝试将我的 JSON 文件映射到一个类对象,然后根据新收到的 JSON 更新卡片。
我的JSON结构是这样的
{
"$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 类创建的对象的字段中?
更新
以下试验在 ci.description 处打印 null,这是否意味着该对象从未创建?
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
}
更新2
打印 cardInfo 给出以下信息:
{$class: FirstCard, id: 1, description: 我是第一卡,........}
请注意,它类似于原始 JSON,但在字符串值上没有双引号。
【问题讨论】: