json - 如何在Dart中创建特定类型的嵌套JSON数据?

标签 json flutter dart

我有一个像这样的模型:

class PotholeData {
    List<Coordinates> coordinates; 
    String location; 
    String image;

    PotholeData({this.coordinates, this.location, this.image});

    PotholeData.fromJson(Map<String, dynamic> json) {
        if (json['coordinates'] != null) {       
            coordinates = new List<Coordinates>();       
            json['coordinates'].forEach((v) {         
            coordinates.add(new Coordinates.fromJson(v));       
            });     
        }     
        location = json['location'];     
        image = json['image'];   
    }

    Map<String, dynamic> toJson() 
    { 
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.coordinates != null) {       
        data['coordinates'] = this.coordinates.map((v) => v.toJson()).toList();     }
        data['location'] = this.location;
        data['image'] = this.image; 
        return data;   
        } 
    }

class Coordinates { 
    String x; 
    String y; 
    String w; 
    String h;

    Coordinates({this.x, this.y, this.w, this.h});

    Coordinates.fromJson(Map<String, dynamic> json) { 
        x = json['x']; 
        y = json['y']; 
        w = json['w']; 
        h = json['h']; 
    }

    Map<String, dynamic> toJson() { 
        final Map<String, dynamic> data = new Map<String, dynamic>(); 
        data['x'] = this.x; 
        data['y'] = this.y;
        data['w'] = this.w;
        data['h'] = this.h; 
        return data;
    } 
}

我正在尝试将这些数据放入Firebase中,而我的方法是:
Map<String, dynamic> potholeData = PotholeData(
    coordinates: sampleCoordinates,
    image: "File name",
    location: "Live Location").toJson();

obj.addData(potholeData).catchError((e) { 
    print(e);
    });     
}

其中sampleCoordinates是类型坐标的列表。但是我没有得到应该包含什么形式的数据。我尝试使用其中的硬编码数据,但是每次输入任何内容时,都会弹出一个错误,指出无法将List / Map / String / int类型的元素分配给列表类型Coordinates。

样本JSON数据如下所示:
{
    "coordinates": [
        {
            "x": "x_coor",
            "y": "y_coor",
            "w": "width",
            "h": "heigth"
        },
        {
            "x": "x_coor",
            "y": "y_coor",
            "w": "width",
            "h": "heigth"
        }
    ],
    "location": "location",
    "image": "image"
}

我需要帮助来了解sampleCoordinates中应包含哪种数据。应该是Map / List / String / int吗? sampleCoordinates已硬编码以供您引用。

我曾尝试将一些数据放入下面,但没有一个起作用。从技术上讲,第一个应该有效。
尝试了以下方法:
List<Coordinates> sampleCoordinates = [{
            "x": "x_coor",
            "y": "y_coor",
            "w": "width",
            "h": "heigth"
        },
        {
            "x": "x_coor",
            "y": "y_coor",
            "w": "width",
            "h": "heigth"
        }];

OR
List<Coordinates> sampleCoordinates = [123,1234];

OR
List<Coordinates> sampleCoordinates = ["asb","adgad"];

最佳答案

带有JSON的用户类转换为:JSON and serialization

class User {
  String name;
  int age;
  User father;

  User({
    this.name,
    this.age,
    this.father,
  });

  factory User.fromJson(String str) => User.fromMap(json.decode(str));

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

  factory User.fromMap(Map<String, dynamic> json) => User(
        name: json["name"],
        age: json["age"],
        father: json["father"] == null ? null : User.fromMap(json["father"]),
      );

  Map<String, dynamic> toMap() => {
        "name": name,
        "age": age,
        "father": father == null ? null : father.toMap(),
      };
}

关于json - 如何在Dart中创建特定类型的嵌套JSON数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60476339/

相关文章:

javascript - 如何修改php中的json_encode函数文件?

android - 此应用程序使用的是已弃用的 Android 嵌入版本

url - 如何在flutter中将参数插入URL中间?

flutter - 没有 RxDart 的 StreamController 的初始值

flutter - 如何在 Flutter 项目中从 Firebase 获取自动生成的 key ?

PHP 循环嵌套 JSON 响应并重新组装为 Webhook 的简单查询字符串

c# - 设置默认全局 json 序列化程序设置

javascript - 如何在没有父/子数据结构的情况下可视化关系?

dart - 在 Flutter 中按后退键关闭抽屉

flutter - 我如何在Flutter中将数据发送到文本字段