首页 > 解决方案 > 显示返回的 JSON

问题描述

我正在尝试在对话框中显示 JSON。无法找到删除显示的括号的方法。任何帮助都会很棒。

在此处输入图像描述

这是 JSON:

{
    "status": false,
    "comment": {
        "email_id": [
            "Email ID \"pixaflip.tech@gmail.com\" has already been taken."
        ],
        
    }
}

这是我为在 Networkutils 类中提取 JSON 而实现的代码:

Future<User> createUser(String url,{Map body}) async{
    return http.post(url,
          body:body).then((http.Response response){
      if (response.statusCode < 200 || response.statusCode > 400 || json == null) {
      throw new Exception("Error while fetching data");
    }
    
    var extractdata = json.decode(response.body);
    if(status == "false"){
       Map comment = extractdata["comment"];
    }
    return User.fromJson(json.decode(response.body));
    });
          
  }


这就是我显示消息的方式:

Networkutils _networkUtils = new NetworkUtils.
var list = _networkUtils.comment.values.toList();
_showDialogue("Registration", "$list");

标签: flutter

解决方案


你可以这样做..

_showDialogue("Registration", "${list[0][0]}");

两对方括号的原因_networkUtils.comment.values.toList()是给你一个列表列表。因此,当您直接将其显示为字符串时,它会显示为 2 对括号,这表明它是一个列表列表。因此,当我们这样做时,list[0][0]它会提取出内部第一个列表的第一项list,这就是您要显示的内容。


推荐阅读