【问题标题】:How to auto format wrong json with code in flutter如何使用颤振中的代码自动格式化错误的json
【发布时间】:2022-06-10 20:22:39
【问题描述】:

我试图解析一个 json 响应。但是,响应末尾有一个逗号,在解码时会引发错误。如果从代码中删除逗号并对其进行验证,我如何自动格式化?

[{"id" : "9991","last_message" : "How about tomorrow then?","members" : ["John", "Daniel", "Rachel"],"topic" : "pizza night", "modified_at" : 1599814026153}, {"id" : "9992","last_message" : "I will send them to you asap","members" : ["Raphael"],"topic" : "slides", "modified_at" : 1599000026153}, {"id" : "9993","last_message" : "Can you please?","members" : ["Mum", "Dad", "Bro"],"topic" : "pictures", "modified_at" : 1512814026153},]

错误

D/EGL_emulation( 7121): app_time_stats: avg=32.94ms min=4.95ms max=83.26ms count=30
E/flutter ( 7121): [ERROR:flutter/shell/common/shell.cc(93)] Dart Unhandled Exception: FormatException: Unexpected character (at character 435)
E/flutter ( 7121): ..."Mum", "Dad", "Bro"],"topic" : "pictures", "modified_at" : 1512814026153},]

【问题讨论】:

  • 你能展示一下你是如何转换然后响应的吗?
  • 完成。请查看

标签: json flutter dart


【解决方案1】:

因此,您应该从源代码中提供一些代码,但我认为您使用的是 http.Response 对象,因此您可以执行以下操作:

String bodyRes = response.body;
bodyRes = bodyRes.endsWith(',]') ? bodyRes.replaceFirst(',]', ']', bodyRes.length - 2) : bodyRes;

【讨论】:

  • 我得到一个错误 Dart Unhandled Exception: type 'Response' is not a subtype of type 'String', stack trace: #0
猜你喜欢
  • 2021-10-07
  • 2021-11-25
  • 2021-02-16
  • 2016-08-20
  • 2021-04-14
  • 2011-03-26
  • 2019-08-18
  • 2014-02-08
  • 2013-03-17
相关资源
最近更新 更多