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