3

I'm working with a RESTful API that seems to be working and used in other applications that gives me something like this:

 "notes": [
    [
      {
        "automaticNote": false,
        "contactId": 0,
        "caseFileId": 0,
        "dateCreated": "2019-05-02",
        "deletedTime": "2019-05-02T19:31:54.588Z"
      }
    ]
  ]

The double pair of square brackets means that one pair of the square brackets has no name/key associated with it. To make matters worse, notes is itself nested in some complex JSON.

I tried using JSON to Dart but it throws an error. So really my question is, how do I serialize a JSON array that has no key/name?

I'd normally do it like this:

    class Note {
  bool automaticNote;
  int contactId;
  int caseFileId;
  String dateCreated;
  String deletedTime;

  Note(
      {this.automaticNote,
      this.contactId,
      this.caseFileId,
      this.dateCreated,
      this.deletedTime});

  Note.fromJson(Map<String, dynamic> json) {
    automaticNote = json['automaticNote'];
    contactId = json['contactId'];
    caseFileId = json['caseFileId'];
    dateCreated = json['dateCreated'];
    deletedTime = json['deletedTime'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['automaticNote'] = this.automaticNote;
    data['contactId'] = this.contactId;
    data['caseFileId'] = this.caseFileId;
    data['dateCreated'] = this.dateCreated;
    data['deletedTime'] = this.deletedTime;
    return data;
  }
}

But the double JSON array is throwing me off (and again, notes itself is nested in a more complex JSON object but for the sake of simplicity I did not include the whole thing here).

Thanks!

2 Answers 2

2

After decoding, notes is a member of some Map<String, dynamic> as usual. Let's call that m.

So m['notes'] is a list, who's first member, m['notes'][0] is also a list. Its first member, m['notes'][0][0] is another Map<String, dynamic>, which is what you need for your constructor.

You should therefore be able to use:

Note n = Note.fromJson(m['notes'][0][0]);
Sign up to request clarification or add additional context in comments.

1 Comment

Wow I think this might solve it! I'll give it a try and let you know. Thank you so much!
1

Let's take the case of you saving automaticNote;

automaticNote = json['notes'][0][0]['automaticNote'];

This offers the key for notes which has an array where your data is at the first point or index 0 (of notes hence [0]) and within that array there is another array which has your data. Again it's the first array item so, [0] and then you're finally at the level with your data so you then use your key for automaticNote, ['automaticNote'].

Note.fromJson(Map<String, dynamic> json) {
    automaticNote = json[0][0]['automaticNote'];
    contactId = json[0][0]['contactId'];
    caseFileId = json[0][0]['caseFileId'];
    dateCreated = json[0][0]['dateCreated'];
    deletedTime = json[0][0]['deletedTime'];
  }

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.