我想用字符串数组解析Flutter中的复杂JSON,然后在另一个listview中显示

时间:2019-04-14 19:23:33

标签: json flutter

所以我想为每个单独的view.builder单元格提取用户名中的第一个字母,即用户内部!

我的问题是我找不到序列化其中具有对象列表的son的任何好方法,这已经成为一个真正的问题!

例如,我们将有一个包含所有类似项目的列表视图,然后显示每个单元格中成员的名称。就像在这种情况下:名称将是:“ test med Teacher chat”,成员将是:“ hugo”和“ studentone”!

This is what I am thinking but the letter being the first letter of every users username!

JSON:

[
{
    "id": 81,
    "users": [
        {
            "username": "hugo",
            "fullname": "Hugo Johnsson"
        },
        {
            "username": "studentone",
            "fullname": "Student One"
        }
    ],
    "title": "test med teacher chat",
    "description": "This project does not have a description.",
    "subject": "No subject",
    "deadline": "2019-01-06",
    "days_left": "98 days ago",
    "overview_requests": [
        {
            "id": 28,
            "user": {
                "username": "hugo",
                "fullname": "Hugo Johnsson"
            },
            "group": 81
        }
    ]
},

未来:

  Future<List<Project>> _getProjects() async {
var data = await http.get(
    "http://studieplaneraren.pythonanywhere.com/api/projects/${UserLog().Username}/?format=json");
var jsonData = json.decode(data.body); //an array of json objects

List<Project> allProjects = [];

for (var JData in jsonData) {
  Project project = Project(
      JData["id"],
      JData["title"],
      JData["description"],
      JData["deadline"],
      JData["subject"],
      JData["days_left"],
      JData["users"]);

  allProjects.add(project);
}

return allProjects;

}

定义:

class Project {
  final int id;
  final String title;
  final String description;
  final String deadline;
  final String subject;
  final String days_left;
  final List users;

  Project(
      this.id,
      this.title,
      this.description,
      this.deadline,
      this.subject,
      this.days_left,
      this.users
  );
}

1 个答案:

答案 0 :(得分:0)

我不太确定我是否了解您要查找的内容,但是如果您试图找到一种解析该json的方法,以便始终获得列表中每个用户名的第一个字母,则只需要了解json的结构以及您正在获取的内容,并确保像平常一样获取所需的字段。

我使用DartPad为您提供了一个小示例,还为您的json添加了更多用户名。

数据

   const List<Map<String,dynamic>> map = [
    {
        "id": 81,
        "users": [
            {
                "username": "hugo",
                "fullname": "Hugo Johnsson"
            },
            {
                "username": "studentone",
                "fullname": "Student One"
            },
            {
                "username": "anotherStudent",
                "fullname": "Student Two"
            },
            {
                "username": "oneMore",
                "fullname": "Student Three"
            },
            {
                "username": "isItEnough",
                "fullname": "Student N"
            }
        ],
        "title": "test med teacher chat",
        "description": "This project does not have a description.",
        "subject": "No subject",
        "deadline": "2019-01-06",
        "days_left": "98 days ago",
        "overview_requests": [
            {
                "id": 28,
                "user": {
                    "username": "hugo",
                    "fullname": "Hugo Johnsson"
                },
                "group": 81
            }
       ]

            }
        ];

获取数据

void main() {

  map.forEach((element) {
    final List<Map<String,dynamic>> users = element['users'] as List;
    users.forEach((user){
      final String firstUsernameLetter = user['username'][0];
      print('First letter of ${user['username']}: $firstUsernameLetter');
    });
  });


}

输出

First letter of hugo: h
First letter of studentone: s
First letter of anotherStudent: a
First letter of oneMore: o
First letter of isItEnough: i