0

I am trying to parse some unique JSON data in my Flutter app.

{
    "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.

2 Answers 2

1

You can use json2dart to convert your json to dart classes even complex and nested json datas will work perfectly.

Here is dart class version of your given json data:

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

how would I go about implementing this into my model?
You can add it as a method, return the data on success and null on error.
This helped. Starting to get this figured out now. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.