FLUTTER:带有对象列表的JSON正文

时间:2019-06-02 09:16:03

标签: json flutter

我是扑扑发展的新手

我想要获取内容JSON正文以及Flutter中的对象列表

像下面的结构一样,

 # remember to depend on both fields
 @api.constrains('installments_calculation','repayment_method')
 def check_installments_calculation(self):
    for rec in self:
        if not rec.installments_calculation and rec.repayment_method == 'salary deduction':
            raise exception.ValidationError(_('You message here'))

任何人都可以建议如何实现这一目标。

{
    "tag1" : "value",
    "parameter" : [ 
        {
            "name":"value1",
            "content":"value2"
        },
        {
            "name":"value1",
            "content":"value2"
        }
                ]
}

预先感谢 沙沙语

1 个答案:

答案 0 :(得分:0)

此JSON数据模型可用于您的情况:

class Response1 {
  final int q;
  final int r;
  final int s;
  final int t;
  final int tYY;
  final double xX;
  final List<Response1ZZ> zZ;

  Response1({this.q, this.r, this.s, this.t, this.tYY, this.xX, this.zZ});

  factory Response1.fromJson(Map<String, dynamic> json) {
    return Response1(
      q: json['Q'] as int,
      r: json['R'] as int,
      s: json['S'] as int,
      t: json['T'] as int,
      tYY: json['TYY'] as int,
      xX: _toDouble(json['XX']),
      zZ: _toObjectList(json['ZZ'], (e) => Response1ZZ.fromJson(e)),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'Q': q,
      'R': r,
      'S': s,
      'T': t,
      'TYY': tYY,
      'XX': xX,
      'ZZ': _fromList(zZ, (e) => e.toJson()),
    };
  }
}

class Response1ZZ {
  final int a;
  final double b;
  final int c;
  final int d;

  Response1ZZ({this.a, this.b, this.c, this.d});

  factory Response1ZZ.fromJson(Map<String, dynamic> json) {
    return Response1ZZ(
      a: json['A'] as int,
      b: _toDouble(json['B']),
      c: json['C'] as int,
      d: json['D'] as int,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'A': a,
      'B': b,
      'C': c,
      'D': d,
    };
  }
}

List _fromList(data, Function(dynamic) toJson) {
  if (data == null) {
    return null;
  }
  var result = [];
  for (var element in data) {
    var value;
    if (element != null) {
      value = toJson(element);
    }
    result.add(value);
  }
  return result;
}

double _toDouble(data) {
  if (data == null) {
    return null;
  }
  if (data is int) {
    return data.toDouble();
  }
  return data as double;
}

List<T> _toObjectList<T>(data, T Function(Map<String, dynamic>) fromJson) {
  if (data == null) {
    return null;
  }
  var result = <T>[];
  for (var element in data) {
    T value;
    if (element != null) {
      value = fromJson(element as Map<String, dynamic>);
    }
    result.add(value);
  }
  return result;
}

/*
Response1:
  "XX": double
  "TYY": int
  "ZZ": List<Response1ZZ>
  "R": int
  "Q": int
  "S": int
  "T": int

Response1ZZ:
  "A": int
  "B": double
  "C": int
  "D": int
*/