[英]How to parse this JSON in dart?
我正在尝试解析 dart 中的以下 JSON 但是每当我运行我的 get 方法时,我的 userPosts object 中什么都没有收到。 我不知道我在这里做错了什么。 我使用 app.quicktype.io 创建了以下 model class
这是我要解析的 JSON。
[
{
"_id": "5eedd6ee16bec833275bc929",
"caption": "Test",
"postImages": "",
"postVideos": "",
"postAudio": "",
"postSharedBy": [
""
],
"tag": [
{
"userId": "",
"name": "",
"guruzziId": ""
}
],
"location": "",
"locationLatLong": [
""
],
"anonymous": false,
"flagInappropriate": [
""
],
"likes": [
""
],
"userId": "5eedbd19996dc053453ff24c",
"createdAt": "2020-06-20 09:29:18",
"updatedAt": "2020-06-20 09:29:18",
"status": true,
"userDetail": {
"_id": "5eedbd19996dc053453ff24c",
"guruzziId": "_bxgza1vgw",
"firstName": "Archit Arora",
"lastName": "",
"photoUrl": ""
}
}
]
这是我创建的模态
import 'dart:convert';
import 'dart:io';
import 'package:flutter/foundation.dart';
List<Post> postFromJson(String str) {
return List<Post>.from(jsonDecode(str).map((x) => Post.fromJson(x)));
}
String postToJson(List<Post> data) =>
json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class Post {
Post({
this.id,
this.caption,
this.postImages,
this.postVideos,
this.postAudio,
this.postSharedBy,
this.tag,
this.location,
this.locationLatLong,
this.anonymous,
this.flagInappropriate,
this.likes,
this.userId,
this.createdAt,
this.updatedAt,
this.status,
this.userDetail,
});
String id;
String caption;
String postImages;
String postVideos;
String postAudio;
List<String> postSharedBy;
List<Tag> tag;
String location;
List<String> locationLatLong;
bool anonymous;
List<String> flagInappropriate;
List<String> likes;
String userId;
String createdAt;
String updatedAt;
bool status;
UserDetail userDetail;
factory Post.fromJson(Map<String, dynamic> json) {
return Post(
id: json["_id"],
caption: json["caption"],
postImages: json["postImages"],
postVideos: json["postVideos"],
postAudio: json["postAudio"],
postSharedBy: List<String>.from(json["postSharedBy"].map((x) => x)),
tag: List<Tag>.from(json["tag"].map((x) => Tag.fromJson(x))),
location: json["location"],
locationLatLong: List<String>.from(json["locationLatLong"].map((x) => x)),
anonymous: json["anonymous"],
flagInappropriate: List<String>.from(json["flagInappropriate"].map((x) => x)),
likes: List<String>.from(json["likes"].map((x) => x)),
userId: json["userId"],
createdAt: json["createdAt"],
updatedAt: json["updatedAt"],
status: json["status"],
userDetail: UserDetail.fromJson(json["userDetail"]),
);
}
Map<String, dynamic> toJson() => {
"_id": id,
"caption": caption,
"postImages": postImages,
"postVideos": postVideos,
"postAudio": postAudio,
"postSharedBy": List<dynamic>.from(postSharedBy.map((x) => x)),
"tag": List<dynamic>.from(tag.map((x) => x.toJson())),
"location": location,
"locationLatLong": List<dynamic>.from(locationLatLong.map((x) => x)),
"anonymous": anonymous,
"flagInappropriate":
List<dynamic>.from(flagInappropriate.map((x) => x)),
"likes": List<dynamic>.from(likes.map((x) => x)),
"userId": userId,
"createdAt": createdAt,
"updatedAt": updatedAt,
"status": status,
"userDetail": userDetail.toJson(),
};
}
class Tag {
Tag({
this.userId,
this.name,
this.guruzziId,
});
String userId;
String name;
String guruzziId;
factory Tag.fromJson(Map<String, dynamic> json) => Tag(
userId: json["userId"],
name: json["name"],
guruzziId: json["guruzziId"],
);
Map<String, dynamic> toJson() => {
"userId": userId,
"name": name,
"guruzziId": guruzziId,
};
}
class UserDetail {
UserDetail({
this.id,
this.guruzziId,
this.firstName,
this.lastName,
this.photoUrl,
});
String id;
String guruzziId;
String firstName;
String lastName;
String photoUrl;
factory UserDetail.fromJson(Map<String, dynamic> json) => UserDetail(
id: json["_id"],
guruzziId: json["guruzziId"],
firstName: json["firstName"],
lastName: json["lastName"],
photoUrl: json["photoUrl"],
);
Map<String, dynamic> toJson() => {
"_id": id,
"guruzziId": guruzziId,
"firstName": firstName,
"lastName": lastName,
"photoUrl": photoUrl,
};
}
这是代码被破坏的 GET 方法
static Future<List<Post>> getPosts() async {
try {
var response = await http.get('$_url/post?userId=$_userId');
if (response.statusCode == 200) {
var data = jsonDecode(response.body)['data'];
List<Post> userPosts = postFromJson(data.toString());
} else {
return List<Post>();
}
} catch (e) {
return List<Post>();
}
}
响应体
{
"data": [
{
"_id": "5eedd6ee16bec833275bc929",
"caption": "Test",
"postImages": "",
"postVideos": "",
"postAudio": "",
"postSharedBy": [
""
],
"tag": [
{
"userId": "",
"name": "",
"guruzziId": ""
}
],
"location": "",
"locationLatLong": [
""
],
"anonymous": false,
"flagInappropriate": [
""
],
"likes": [
""
],
"userId": "5eedbd19996dc053453ff24c",
"createdAt": "2020-06-20 09:29:18",
"updatedAt": "2020-06-20 09:29:18",
"status": true,
"userDetail": {
"_id": "5eedbd19996dc053453ff24c",
"guruzziId": "_bxgza1vgw",
"firstName": "Archit Arora",
"lastName": "",
"photoUrl": ""
}
}
],
"error": false,
"message": "Success",
"code": 200
}
您的问题是您得到的响应不是 JSON ,其中包含表示为字符串的 JSON 。 相反,整个响应是一个组合的 JSON 所以你不需要解析它两次。
我创建了以下内容,显示了您需要做什么。 json
是一个字符串,其值为您提供的“响应正文”。 自从我在那里进行了更改后,我还包括postFromJson
:
void main() {
dynamic data = jsonDecode(json)['data'];
List<Post> userPosts = postFromJson(data);
userPosts.forEach((x) => print(x.toJson())); // just to show each object
}
List<Post> postFromJson(List<dynamic> postJsonObjs) {
return List<Post>.from(postJsonObjs.map((x) => Post.fromJson(x)));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.