【问题标题】:Obtain Inner JSON Array Values获取内部 JSON 数组值
【发布时间】:2014-10-08 05:40:15
【问题描述】:

我正在尝试获取 serviceResponseValue 映射中“filterInputParameters”数组的值。现在我试图遍历地图,但只能获得第一级数据,例如 displayName,我需要更深入地了解 filterInputParamters 数组中的值。如果您需要更多信息,请告诉我。

飞镖代码:

var jsonString = response;

   var dropDown = querySelector("#asset");

   Map jsonObject = JSON.decode(jsonString) as Map;
       dropDownList = jsonObject["serviceResponseValue"] as List<Map>;

   LinkedHashMap<String, Map> dataMap = new LinkedHashMap<String, Map>();

//the one causing issues and returning null
     var ddValues2 = dropDownList
       //extract the 'displayValue'
       .map((e2) => e2['filterInputParameters']['value']);
       //create a set to eliminate duplicates
       //.toSet().toList()
       //sort the result 
       //..sort();

       ddValues2.forEach((e2) {
         print(e2);

       });

【问题讨论】:

  • 访问 json.org 并学习 JSON 语法 -- 只需 5-10 分钟即可学会,然后事情就会变得更有意义。

标签: java arrays json map dart


【解决方案1】:
Map jsonObject = JSON.decode(jsonString) as Map;
print(jsonObject["serviceResponseValue"][0]["filterInputParameters"]);

在 JSON 中,[ ] 表示一个列表,{ } 表示一个地图。
您可以通过传递数字索引来访问列表元素(xxx[5] 以获取第 6 项)
和一个用于访问地图项的字符串 (xxx["serviceResponeValue"])。

您的 JSON 以

开头
 {  // the outer element is a map
   "serviceResponseValue":[  // this map item can be accessed with a 
                             // string index"serviceResponseValue"
                             // after the colon `:` starts the associated value, a list
                             // the first item can be accessed using [0]
      {  // which contains a map

...

       "filterInputParameters":[  // this item of the map is returned by ["filterInputParameters"]
          {  
             "id":"8a4984e047d0e40d0147d0e410020008",

【讨论】:

  • 问题,我将如何获得第二张价值地图,上面写着 48 小时等?我试过 jsonObject["serviceResponseValue"][2]["filterInputParameters"];没有运气。
  • jsonObject["serviceResponseValue"][1]
猜你喜欢
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
  • 2019-07-04
  • 2013-04-04
  • 2016-12-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多