如何使用Flutter提取JSON数据

huangapple go评论62阅读模式
英文:

how to extract a json data using flutter

问题

在我的Flutter应用中,我有一个JSON对象的列表。我使用这些对象来获取其他数据。返回类型是 Map<int, List<int>>,对象结构如下:

{1:[1,7], 2:[1,141]}
{2:[142, 252]}
{2:[253, 286], 3:[1,92]}
...

这是它的返回内容。第一个数字表示章节,数组表示节的起始和结束。所以我想要做的是分别获取章节名称、节的起始和结束。

那么,如何遍历这些数据呢?

英文:

In my fluter app i have a list of json object. and with this object i use it to fetch other datas.the return type is Map&lt;int, List&lt;int&gt;&gt;, and the object structure is as follows.

{1:[1,7], 2:[1,141]}
{2:[142, 252]}
{2:[253, 286], 3:[1,92]}
...

this is what it returns. the first number indicates the chapter, the arrays are the verse start and end. so what i wanted todo with this is to get the chapter names, the verse start and end, separately.

so how can i map through this data?

答案1

得分: 1

最简单的方法是遍历data

输出:

Chapter 1 verse [1, 7]
Chapter 2 verse [1, 141]
Chapter 2 verse [142, 252]
Chapter 2 verse [253, 286]
Chapter 3 verse [1, 92]
英文:

The simplest answer would be iterating through the data.

List&lt;Map&lt;int, List&lt;int&gt;&gt;&gt; data = [
  {1:[1,7], 2:[1,141]},
  {2:[142, 252]},
  {2:[253, 286], 3:[1,92]}
];

for (var item in data) {
  item.forEach((key, value) {
    print(&#39;Chapter $key verse $value&#39;);
  });
}

Output:

Chapter 1 verse [1, 7]
Chapter 2 verse [1, 141]
Chapter 2 verse [142, 252]
Chapter 2 verse [253, 286]
Chapter 3 verse [1, 92]

huangapple
  • 本文由 发表于 2023年3月12日 08:03:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710313.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定