【问题标题】:I'm trying to prase the Json data to model class in Flutter. but i'm getting "Flutter FormatException: Unexpected character" Error我正在尝试将 Json 数据解析为 Flutter 中的模型类。但我收到“Flutter FormatException:意外字符”错误
【发布时间】:2021-12-26 16:44:53
【问题描述】:

我的代码:

Future<List<Quections>?> getSiteQuection(String SiteId, SuperisorID) async {
  var data = await http.post(
      Uri.parse("https://churchiest-dump.000webhostapp.com/question.php"),
      headers: {'Content-Type': 'application/json', 'Charset': 'utf-8'},
      body: jsonEncode({
        "site_id": SiteId,
        "date": DateFormat.yMd().format(DateTime.now()),
        "supervisor_id": SuperisorID
      }));
  if (data.statusCode == 200) {
    print(data.body);
    var jsonResponse = json.decode(data.body);
    return (jsonResponse as List<dynamic>?)
        ?.map((e) => Quections.fromJson(e as Map<String, dynamic>))
        .toList();
  } else {
    print("faild to response Quection list :" + data.body);
  }
}

您还想知道下面插入的值是否可以用于snapshot.data [index].inserted 在 getSiteQuection Future 构建器中将其作为 Future 后。

class Quections {
  Quections({
    this.the0,
    this.questionId = '',
    this.question = '',
  });

  The0? the0;
  String questionId;
  String question;

  factory Quections.fromRawJson(String str) =>
      Quections.fromJson(json.decode(str));

  String toRawJson() => json.encode(toJson());

  factory Quections.fromJson(Map<String, dynamic> json) => Quections(
        the0: The0.fromJson(json["0"]),
        questionId: json["question_id"],
        question: json["question"],
      );

  Map<String, dynamic> toJson() => {
        "0": the0!.toJson(),
        "question_id": questionId,
        "question": question,
      };
}

class The0 {
  The0({
    this.inserted = '',
  });

  String inserted;

  factory The0.fromRawJson(String str) => The0.fromJson(json.decode(str));

  String toRawJson() => json.encode(toJson());

  factory The0.fromJson(Map<String, dynamic> json) => The0(
        inserted: json["inserted"],
      );

  Map<String, dynamic> toJson() => {
        "inserted": inserted,
      };
}

Json

{
    "question_id": "2",
    "question": "Test question"
}{
    "question_id": "3",
    "question": "is it clean?",
    "0": {
        "inserted": "1"
    }
}{
    "question_id": "4",
    "question": "is it good looking?",
    "0": {
        "inserted": "1"
    }
}

这是错误: https://i.stack.imgur.com/Du7hs.png

【问题讨论】:

  • 你试过了吗?如果没有试试stackoverflow.com/questions/55671441/…
  • 那个 JSON 确实是无效的。你不能有{...}{...} = 即连续两个对象。这个无效 JSON 的来源是什么?
  • @shanmkha 谢谢。该解决方案有效。但我得到另一个错误'发生异常。 _CastError(类型 'String' 不是类型转换中类型 'List?' 的子类型)'
  • 尝试将“String”替换为“var”或List

标签: json flutter formatexception


【解决方案1】:

您的 json 无效。 Json 应该有一个根。例如,它可以是一个数组元素

[
  {
    "question_id": "2",
    "question": "Test question"
  },
  .....
  {
    "0": {
      "inserted": "1"
    },
    "question_id": "4",
    "question": "is it good looking?"
  }
]

而且对象之间应该有逗号“,”

【讨论】:

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