gpt4 book ai didi

java - Flutter如何解析JSON Mongodb数据

转载 作者:行者123 更新时间:2023-12-01 16:53:39 24 4
gpt4 key购买 nike

我正在尝试使用 mongodb 解析一些 JSON 数据。我没有找到数据我认为问题在于 mongodb 数据中的字段差异

{"imei":"865566048694354","_id":"5e7c996fd6eb5f039c50bd26","createdAt":"2020-03-26T12:00:47.021Z","updatedAt":"2020-03-26T12:00:47.021Z","__v":0},{"imei":{"test":{"tactileState":"ignore","pixelState":"ignore"},"name":"h12hhhhgkhh"},"_id":"5ea8357d8c562b3dd8fe5bf1","createdAt":"2020-04-28T13:54:05.094Z","updatedAt":"2020-04-28T13:54:05.094Z","__v":0},{"imei":{"test":{"tactileState":"ignore","pixelState":"ignore"},"name":"h12hhhhgkhh"},"_id":"5ea8366741a5e527446744a2","createdAt":"2020-04-28T13:57:59.035Z","updatedAt":"2020-04-28T13:57:59.035Z","__v":0},{"imei":{"test":{"tactileState":"ignore","pixelState":"ignore","greyState":"ignore"},"name":"h12hhhhgkhh"},"_id":"5ea837614cf7ed30f0163c38","createdAt":"2020-04-28T14:02:09.395Z","updatedAt":"2020-04-28T14:02:09.395Z","__v":0},{"imei":{"test":{"bafleState":"1","microState":"1","vibreurState":"1"},"name":"h12hhhhgkhh"},"_id":"5ea837854cf7ed30f0163c39","createdAt":"2020-04-28T14:02:45.287Z","updatedAt":"2020-04-28T14:02:45.287Z","__v":0}

如果你能帮助我如何编写类以及如何在flutter中编写方法,因为我制作的所有内容总是快照没有数据,我认为mongodb数据中字段差异的问题导致了问题,因为所有教程我看到的文章没有使用不同的数据库字段,即使使用嵌入文档,也始终具有相同的结构。

最佳答案

假设你有一个像这样的json

{
"name": "John Smith",
"email": "john@example.com"
}

使用 `dart:convert,您可以通过两种方式序列化此 JSON 模型。

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

或者创建一个像这样的模型

class User {
final String name;
final String email;

User(this.name, this.email);

User.fromJson(Map<String, dynamic> json)
: name = json['name'],
email = json['email'];

Map<String, dynamic> toJson() =>
{
'name': name,
'email': email,
};
}

然后像这样使用它:

Map userMap = jsonDecode(jsonString);
var user = User.fromJson(userMap);

print('Howdy, ${user.name}!');

引用号是here .

关于java - Flutter如何解析JSON Mongodb数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61633504/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com