0

I have troubles with parsing a JSON file with array.

It looks like something like this:

{
    "status": "200",
    "addresses": [
        {
            "address": "Address 1"
        },
        {
            "address": "Address 2"
        }
    ]
}

And I tried to parse it with:

var response = jsonDecode(res.body);
print(response['addresses']['address'][0]);
print(response['addresses']['address'][1]);

But it is not working. Is there any common pattern how this should be?

2
  • It is not working because your code is incorrect. Why do you think that it should work? Commented Jul 27, 2019 at 8:32
  • Thank you Captain Obvious :D. I didn't say it should work... I am asking a question how can I correct my code which is a main purpose of this website :D. Commented Jul 27, 2019 at 12:57

3 Answers 3

2

That's because you're not accessing it the right way. You have a Map<String,dynamic> that has a List<Map<String,String>> for the key addresses.

If you want to access the first two elements of that list, you can do it by doing:

var response = jsonDecode(res.body);
print(response['addresses'][0]['address']);
print(response['addresses'][1]['address']);
Sign up to request clarification or add additional context in comments.

Comments

1

The easiest way I have found for dealing with this is to have this website write the JSON parser for me. Simply copy / paste you JSON into provide field and choose Dart as the language:

https://app.Quicktype.io

Comments

1

Your best mapping the data into a class there is a useful website (created by Javier Lecuona) that generates the class for you. https://javiercbk.github.io/json_to_dart/

Here is an example:

var parsedJson = jsonDecode(json);
var addressList = ClientAddresses.fromJson(parsedJson);
print(addressList.addresses[0].address);
print(addressList.addresses[1].address);

class ClientAddresses {
  String status;
  List<Addresses> addresses;

  ClientAddresses({this.status, this.addresses});

  ClientAddresses.fromJson(Map<String, dynamic> json) {
    status = json['status'];
    if (json['addresses'] != null) {
      addresses = new List<Addresses>();
      json['addresses'].forEach((v) {
        addresses.add(new Addresses.fromJson(v));
      });
    }
  }

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

class Addresses {
  String address;

  Addresses({this.address});

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

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

Comments

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.