简体   繁体   English

Dart/Flutter:解析 Json object

[英]Dart/Flutter: Parsing Json object

I am trying to parse some unique JSON data in my Flutter app.我正在尝试在我的 Flutter 应用程序中解析一些独特的 JSON 数据。

{
    "NewDataSet": {
        "Route": [
            {
                "RouteID": "1",
                "RouteNum": "20",
                "Description": "NORTH DAKOTA "
                
            },
            {
                "RouteID": "11",
                "RouteNum": "25",
                "Description": "East SD "
            }
        ]
    }
}

I am not sure how to parse this with two objects.我不确定如何用两个对象解析它。

You can chain the square brackets and simply refer to the respective names, as long as you know the data you're receiving.只要您知道收到的数据,您就可以链接方括号并简单地引用各自的名称。

import 'dart:convert';

void main() {
  decodeJSON();
}

void decodeJSON() async {
  String data =
        '{"NewDataSet": {"Route": [{"RouteID": "1","RouteNum": "20","Description": "NORTH DAKOTA "},{"RouteID": "11","RouteNum": "25","Description": "East SD "}]}}';

    try {
      dynamic decoded = await jsonDecode(data);
      dynamic newDataSet = await decoded['NewDataSet'];
      dynamic routes = await decoded['NewDataSet']['Route'];

      print(newDataSet);
      print(routes);
    } catch (e) {
      print(e);
    }
}

You can use json2dart to convert your json to dart classes even complex and nested json datas will work perfectly.您可以使用json2dart将您的 json 转换为 dart 类,即使是复杂和嵌套的 json 数据也可以正常工作。

Here is dart class version of your given json data:这是给定 json 数据的 dart class 版本:

class Autogenerated {
  NewDataSet newDataSet;

  Autogenerated({this.newDataSet});

  Autogenerated.fromJson(Map<String, dynamic> json) {
    newDataSet = json['NewDataSet'] != null
        ? new NewDataSet.fromJson(json['NewDataSet'])
        : null;
  }

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

class NewDataSet {
  List<Route> route;

  NewDataSet({this.route});

  NewDataSet.fromJson(Map<String, dynamic> json) {
    if (json['Route'] != null) {
      route = new List<Route>();
      json['Route'].forEach((v) {
        route.add(new Route.fromJson(v));
      });
    }
  }

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

class Route {
  String routeID;
  String routeNum;
  String description;

  Route({this.routeID, this.routeNum, this.description});

  Route.fromJson(Map<String, dynamic> json) {
    routeID = json['RouteID'];
    routeNum = json['RouteNum'];
    description = json['Description'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['RouteID'] = this.routeID;
    data['RouteNum'] = this.routeNum;
    data['Description'] = this.description;
    return data;
  }
}

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

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