flutter 如何在应用程序UI中显示接收到的JSON

a11xaf1n  于 12个月前  发布在  Flutter
关注(0)|答案(1)|浏览(107)

我从API收到一个json,如下所示,我如何将其显示为列?
键和值根据错误的发生而改变。

{"message":{"name":["The name field must be at least 5 characters."],"mobile":["The mobile field format is invalid.","The mobile field must be a number.","The mobile field must have at least 11 digits.","The mobile field must not have more than 11 digits."]}}
ie3xauqp

ie3xauqp1#

@Mahdi Msb请检查以下代码。我相信这可能会有帮助。

class JsonDataColumn extends StatelessWidget {
  String jsonString = '''
      {
        "data": {
          "key_1": ["Value of key 1"],
          "key_2": [
            "1st Value of key 2",
            "2nd Value of key 2",
            "3rd Value of key 2"
          ]
        }
      }
    ''';

  @override
  Widget build(BuildContext context) {
    Map<String, dynamic> jsonData = json.decode(jsonString);
    Map<String, dynamic> messages = jsonData['data'];

    return MaterialApp(
      home: Scaffold(
        body: ListView(
          children: messages.entries.map((entry) {
            String key = entry.key;
            List<dynamic> values = entry.value;
            return Column(
              children: [
                Text(
                  'Key: $key',
                  style: const TextStyle(fontWeight: FontWeight.bold),
                ),
                for (var value in values) Text('Value: $value'),
              ],
            );
          }).toList(),
        ),
      ),
    );
  }
}

相关问题