【问题标题】:How do I decode this response body?如何解码此响应正文?
【发布时间】:2019-06-15 07:14:47
【问题描述】:

我有这个来自 http 推送的响应正文

"{"标识符":"00000000-0000-0000-0000-00000000000"}"

我想将 000000... 部分作为字符串

这是我的代码的相关部分

.

.. async { 
    await http
          .post(Uri.encodeFull(mainURL + registrEndPoint + _stuff))
          .then((res) {
        if (res.statusCode == 202) {  
        Map _body = jsonDecode(res.body); 
                           // I checked debugging, the respons boy is ok
        String _id =_body['identifier'];
        return _id;
}...

我相信我在“映射”中遗漏了一些东西
我怀疑组合'quote-curlyBraces-quote' 打败我的 jsonDecode;

有什么建议吗?

提前致谢

【问题讨论】:

  • String _id =_body['identifier']; 能得到什么?
  • 似乎为空,当我调试时它只是跳过断点
  • 使用调试器应该不难调查_body中的实际内容。
  • '悬停'鼠标在上面不显示任何内容,跳过下一步,返回为空

标签: json http dart flutter decode


【解决方案1】:

通过查看dart:convert 文档,您会看到 jsonDecode() 返回一个 Map<String, dynamic>,表示直到运行时才知道值的类型。

Map<String, dynamic> body = jsonDecode(jsonString);
print('Howdy, ${body['identifier']}!');

【讨论】:

  • 感谢您的回答,但这似乎正是我已经在做的[编辑:我在调试时看到运行时返回的值,res body 值是初始帖子中的一个引号,但为空]
【解决方案2】:

我解决了添加这一行的问题

String _body = res.body;

如下所述,

await http
        .post(Uri.encodeFull(mainURL + registrEndPoint + _qr))
        .then((res) {
      if (res.statusCode == 202) {
        String _body = res.body;   //<--- HERE!
        Map _json = jsonDecode(_body);
        String _id = _json['identifier'];
        return _id ;
    });

谢谢大家的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多