将JSON列表字符串转换为JSON对象颤振格式异常:意外字符(字符2处)



我正在调用一个返回字符串的端点。如何将字符串格式化为模型,响应是一个对象列表。

因此,在我的代码中,我将响应的格式映射为

//response from server
[
{
"name" : "Test Test",
"age" : 10
},
{
"name" : "Test Test",
"age" : 10
}
]
But I formatted it to look as below
Map<String, dynamic> myResponse = {"status": true, "data":  responseFromServer};
Response model = Response.fromJson(jsonDecode(result));

但是我得到的错误如下

I/flutter ( 7718): FormatException: Unexpected character (at character 2)
I/flutter ( 7718): {status: true, data: [{"key":127,"name":"Statement 3!","clientFullName":nul...
I/flutter ( 7718):  ^

这是我的模型

// To parse this JSON data, do
//
//     final response = responseFromJson(jsonString);
import 'dart:convert';
Response responseFromJson(String str) => Response.fromJson(json.decode(str));
String responseToJson(Response data) => json.encode(data.toJson());
class Response {
Response({
this.status,
this.data,
});
bool status;
List<Datum> data;
factory Response.fromJson(Map<String, dynamic> json) => Response(
status: json["status"],
data: List<Datum>.from(json["data"].map((x) => Datum.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"status": status,
"data": List<dynamic>.from(data.map((x) => x.toJson())),
};
}
class Datum {
Datum({
this.name,
this.age,
});
String name;
int age;
factory Datum.fromJson(Map<String, dynamic> json) => Datum(
name: json["name"],
age: json["age"],
);
Map<String, dynamic> toJson() => {
"name": name,
"age": age,
};
}

我想要实现的是将响应传递给模型并将模型返回给被使用的对象。我做错了什么?

这里有两点:

  • 请分享CustomModel的代码以便进一步评估。
  • 使用这个简单的工具来创建JSON来插入类并在几秒钟内集成。

最新更新