简体   繁体   English

Flutter 如何解析 JSON Mongodb 数据

[英]Flutter how to parse JSON Mongodb data

I'm trying to parse some JSON data with mongodb.我正在尝试使用 mongodb 解析一些 JSON 数据。 I have no data found I think that the problem is with the difference in field in mongodb data我没有找到数据我认为问题在于 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}
   

If you can help me how to write the class and how to write the method in flutter because everything which I made always snapshot has no data I think that the problem in the difference in fields in mongodb data makes the problem because all tutorial and article which I see didn't use different database field always the same structure even with embedded document.如果你能帮助我如何编写 class 以及如何在 flutter 中编写方法,因为我制作的所有内容总是快照没有数据我认为 mongodb 数据中的字段差异问题是因为我看到即使使用嵌入式文档,也没有使用不同的数据库字段总是相同的结构。

Suppose you have a json like this假设你有一个像这样的 json

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

With `dart:convert, you can serialize this JSON model in two ways.使用`dart:convert,你可以通过两种方式序列化这个JSON model。

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

Or create a model like this或者像这样创建一个 model

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,
};
}

And then use it like this:然后像这样使用它:

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

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

Reference is here .参考在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM