java - 获取内部 JSON 数组值

标签 java arrays json dictionary dart

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

Dart 代码:

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);

       });

最佳答案

Map jsonObject = JSON.decode(jsonString) as Map;
print(jsonObject["serviceResponseValue"][0]["filterInputParameters"]);

在 JSON [ ] 中表示一个 List 和 { } 一个 Map。
您可以通过传递数字索引来访问列表元素(xxx[5] 以获取第 6 项)
和一个用于访问 map 项的字符串 (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",

关于java - 获取内部 JSON 数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25310276/

相关文章:

java - 使用 Java 监视 tcpflow 输出?

java - eclipse 调试器 - 在特定位置引发异常时的断点

java - Java Web 应用程序中的 manifest.txt 文件的用途是什么?

javascript - 摘要循环显示我的数组没有变化(并且 View 不刷新)

java - 无法弄清楚 NullPointerException 从哪里来

python - 快速随机配对排列

java - java中元素未写入数组

ios - 如何编写一个将字符串和泛型类型作为参数的函数?

javascript - 读取从 API REST 返回的 JSON 对象

python - 格式化嵌套的 json 以用于 Python 请求