我正在尝试从JSON中的嵌套数组中提取数据,但似乎无法正确获取值。现在,当我只需要操作系统的名称时,嵌套的OperatingSystem数组的所有值就会在表中打印出来。我的代码在下面,如果您需要更多信息,请让我。
镖:
List<Map> assetList;
// LinkedHashMap preserves key entry order
LinkedHashMap<String, Map> dataMap = new LinkedHashMap<String, Map>();
for (var d in assetList) {
HashMap rowMap = new HashMap();
String domainId = d["process"]["processId"];
//first <td> element, the rest follow in succession
dataMap[domainId] = rowMap;
rowMap["domainId"] = domainId;
//is still not checking if null
if(d["asset"]["operatingSystem"].containsKey("name")){
rowMap["operatingSystem"] = d["asset"]["operatingSystem"]["name"];
} else{
rowMap["operatingSystem"] = d["asset"]["operatingSystem"];
}
//print out table data for debugging
print(rowMap.toString());
print(d);
JSON:
"asset":{
"assetId":"8a498592469189660146918d9e2f0000",
"oplock":0,
"domainName":"",
"latitude":58.92,
"ipAddress":"4.4.4.4",
"longitude":-37.23,
"operatingSystem":{
"osId":2,
"oplock":0,
"name":"Windows 8"
}
}
最佳答案
您需要更深一层。您正在打印出对象的OperatingSystem header ,但operatingSystem header 具有3个属性。
corect语法是
json["asset"]["operatingSystem"]["name"];
您也可以这样做,我认为这在JS和JSON上是更标准的
json.asset.operatingSystem.name
关于json - Google Dart JSON提取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25409585/