我試圖將我的JSON文件映射到一個類對象,然後基於新接收的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空,這是否意味着該對象從未被創造出來的?
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給出如下:
{$類:FirstCard,ID:1,描述:我的卡號一個,...... ..}
請注意,它類似於原始的JSON,但沒有字符串值的雙引號。
請檢查我的文章中更新的部分,我嘗試打印對象ci中的一個字段,我得到的全部爲空。 – aziza
'print(cardInfo);'print? –
請在原始文章中查看我的新更新。 – aziza