简体   繁体   English

在 Dart/Flutter 中解析嵌套的 JSON

[英]Parse nested JSON in Dart/Flutter

I'm trying to parse my JSON into Data class object. I did some research and did come upon some helpful articles but am still running into errors.我正在尝试将我的 JSON 解析为数据 class object。我做了一些研究,确实发现了一些有用的文章,但仍然遇到错误。

The response I get from the backend:我从后端得到的响应:

  "entrances": {
    "options": {
      "mainEntrance": {
        "text": "Enter through the main doors"
      },
      "backEntrance": {
        "text": "Enter through the back doors"
      },
      "sideEntrance": {
        "text": "Enter through the side doors"
      }
    },
    "footer": "Text goes here"
  },
  "status": {
    "state": "open"
  }
}

Model Model

class MyClass {
  String id;
  Options option;

  MyClass({this.id, this.option});

  MyClass.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    option = json['option'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['option'] = this.option;
    return data;
  }
}

class Options {
  String name;
  String text;

  Options({this.name, this.text});

  Options.fromJson(Map<String, dynamic> json) {
    name = json['name'];
    text = json['text'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['name'] = this.name;
    data['text'] = this.text;
    return data;
  }
}

Decoding the response and mapping解码响应和映射

      setState(() {

        Map myMap = json.decode(response.body);
        List<MyClass> myClassList = new List<MyClass>();
        myMap['entrances'].forEach((key, value) {
          value['id'] = key;
          myClassList.add(MyClass.fromJson(value));
        });

        myClassList.forEach((MyClass) {
          print(MyClass.id);
          print(MyClass.option.name);
          print("--------------------\n");
        });
      });

I get this error:我收到此错误:

Unhandled Exception: NoSuchMethodError: Class 'String' has no instance method '[]='.
Tried calling: []=("id", "footer")

What am I doing wrong?我究竟做错了什么? Are there better approaches out there?有更好的方法吗?

You can use this website to convert your JSON to a Dart model. I assume that your JSON starts with curly brackets.您可以使用此网站将您的 JSON 转换为 Dart model。我假设您的 JSON 以大括号开头。


{
    "entrances": {
        "options": {
            "mainEntrance": {
                "text": "Enter through the main doors"
            },
            "backEntrance": {
                "text": "Enter through the back doors"
            },
            "sideEntrance": {
                "text": "Enter through the side doors"
            }
        },
        "footer": "Text goes here"
    },
    "status": {
        "state": "open"
    }
}

Then your model should look like:那么你的 model 应该是这样的:


class ResponseModel {
  Entrances entrances;
  Status status;

  ResponseModel({this.entrances, this.status});

  ResponseModel.fromJson(Map<String, dynamic> json) {
    entrances = json['entrances'] != null
        ? new Entrances.fromJson(json['entrances'])
        : null;
    status =
        json['status'] != null ? new Status.fromJson(json['status']) : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.entrances != null) {
      data['entrances'] = this.entrances.toJson();
    }
    if (this.status != null) {
      data['status'] = this.status.toJson();
    }
    return data;
  }
}

class Entrances {
  Options options;
  String footer;

  Entrances({this.options, this.footer});

  Entrances.fromJson(Map<String, dynamic> json) {
    options =
        json['options'] != null ? new Options.fromJson(json['options']) : null;
    footer = json['footer'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.options != null) {
      data['options'] = this.options.toJson();
    }
    data['footer'] = this.footer;
    return data;
  }
}

class Options {
  MainEntrance mainEntrance;
  MainEntrance backEntrance;
  MainEntrance sideEntrance;

  Options({this.mainEntrance, this.backEntrance, this.sideEntrance});

  Options.fromJson(Map<String, dynamic> json) {
    mainEntrance = json['mainEntrance'] != null
        ? new MainEntrance.fromJson(json['mainEntrance'])
        : null;
    backEntrance = json['backEntrance'] != null
        ? new MainEntrance.fromJson(json['backEntrance'])
        : null;
    sideEntrance = json['sideEntrance'] != null
        ? new MainEntrance.fromJson(json['sideEntrance'])
        : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.mainEntrance != null) {
      data['mainEntrance'] = this.mainEntrance.toJson();
    }
    if (this.backEntrance != null) {
      data['backEntrance'] = this.backEntrance.toJson();
    }
    if (this.sideEntrance != null) {
      data['sideEntrance'] = this.sideEntrance.toJson();
    }
    return data;
  }
}

class MainEntrance {
  String text;

  MainEntrance({this.text});

  MainEntrance.fromJson(Map<String, dynamic> json) {
    text = json['text'];
  }

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

class Status {
  String state;

  Status({this.state});

  Status.fromJson(Map<String, dynamic> json) {
    state = json['state'];
  }

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


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

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