json - Google Dart JSON提取

标签 json dart

我正在尝试从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/

相关文章:

ListFragment 中的 android JSON

php - MySQL 表和 JSON 解析。是否有外键?

json - 如何使用 Oracle 12C JSON_VALUE 提取大于 4000 字节的 json 值?

java - 在 Struts 1.3 中处理从 Ajax 发布的 JSON 数组

dart - 在 Flutter 中,Dependencies 必须指定版本号?

android - Dart 中的参数化字符串变量

angularjs - Angular Dart 和表单验证

javascript - Ngresource指定不同的URL的get all和get one

dart - flutter 中的答案颜色?

firebase - forEach 在 flutter 中出错以显示带有来自 firebase 实时数据的 ListView