我是个新手。我试图从jsonDecoded变量中获取所有键。我已经尽一切所能来获取这些密钥。但是我做不到。

所以我的问题是如何以最简单,最短的方式从jsonDecoded值中获取所有密钥?

这是我的代码:

    Future<String> getData() async {
    var response = await http.get(
        Uri.encodeFull(
            "https://my-json-server.typicode.com/dipcse07/demoJSON/db"),
        headers: {"Accept": "application/json"});

    //print(response.body);
    print('tapped');

    setState(() {
      data = jsonDecode(response.body.toString());

      collegeDataLenght = data['CollegeName1'].length;
      count = collegeDataLenght;
      teachersInfo = data['CollegeName1'];
      print(teachersInfo[0]['avatarImage']);
 });
}

*****需要从data获取所有密钥**

我从我的api链接获取的数据是
{
   "CollegeName1":[
      {
         "teacherName":"Md Sazid hasan dip",
         "teacherID":"2345",
         "avatarImage":"url",
         "address":"9/2, shenbari Road, Shanki Para Bazar Mymensingh",
         "status":"Active",
         "joinDate":"date",
         "resignDate":"present"
      },
      {
         "TeacherName":"Talha Azadi",
         "teacherID":"2345",
         "avatarImage":"url",
         "address":"9/2, shenbari Road, Shanki Para Bazar Mymensingh",
         "status":"Active",
         "joinDate":"date",
         "resignDate":"present"
      }
   ],
   "CollegeName2":[
      {
         "teacherName":"Md Sazid hasan dip",
         "teacherID":"2345",
         "avatarImage":"url",
         "address":"9/2, shenbari Road, Shanki Para Bazar Mymensingh",
         "status":"Active",
         "joinDate":"date",
         "resignDate":"present"
      },
      {
         "TeacherName":"Talha Azadi",
         "teacherID":"2345",
         "avatarImage":"url",
         "address":"9/2, shenbari Road, Shanki Para Bazar Mymensingh",
         "status":"Active",
         "joinDate":"date",
         "resignDate":"present"
      }
   ]
}

最佳答案

您解析的json将返回 Map ,它具有 .keys 属性,可以使用:

void main() {
  final parsedJson = jsonDecode(json) as Map<String, dynamic>;
  print(parsedJson.keys);
}

09-26 03:10