You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中Supabase返回无效JSON,如何转换为有效格式?

解决Flutter中Supabase返回数据的JSON格式问题

你看到的“无效JSON”其实是Dart对象的打印格式,不是真的无效响应——Supabase Dart SDK会自动把API返回的JSON解析成List<Map<String, dynamic>>类型的Dart对象,直接打印这个对象时,输出的是Dart的字符串表示(不带引号),而非标准JSON格式。

两种处理方式:

  • 转成标准JSON字符串:如果需要获取带引号的有效JSON,使用dart:convert里的jsonEncode方法将解析后的对象转换:

    import 'dart:convert';
    
    try {
      var response = await SupabaseCredentials.supabaseClient
          .from("emp_table")
          .select();
      String validJson = jsonEncode(response);
      print(validJson);
    } catch (e) {
      print(e.toString());
    }
    

    执行后会输出标准JSON格式:

    [{"id":1,"created_at":"2023-01-12T10:59:08.631522+00:00","emp_designation":"Technical","emp_username":"fzn","emp_contact":"+91989274","emp_email":"m.developer0@gmail.com","emp_address":"Uttar Pradesh."}]
    
  • 直接操作Dart对象:如果只是要处理数据,不需要转JSON,直接用Dart的List/Map语法操作即可:

    try {
      var employees = await SupabaseCredentials.supabaseClient
          .from("emp_table")
          .select();
      // 遍历并使用数据
      for (var emp in employees) {
        print('ID: ${emp['id']}, 职位: ${emp['emp_designation']}');
      }
    } catch (e) {
      print(e.toString());
    }
    

内容的提问来源于stack exchange,提问作者Mohammad Faizan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 19:55:50