Json_serializable.dart: How do I deserialize a JSONArray?

Created on 12 Apr 2018  Â·  5Comments  Â·  Source: google/json_serializable.dart

Whats the best way to do this? The solution I found right now is pretty crappy?

 http.Response resp = await http.get('$endpoint');
List l = json.decode(resp.body);
    List<Message> c = new List();
    l.forEach((i){
      c.add(new Message.fromJson(i));
      return i;
    });

    return c;

Most helpful comment

import 'dart:convert';

main() async {
  var stuff;
  var myThing = (JSON.decode(stuff) as List).map((e) => new MyClass.fromJson(e)).toList();
}

All 5 comments

import 'dart:convert';

main() async {
  var stuff;
  var myThing = (JSON.decode(stuff) as List).map((e) => new MyClass.fromJson(e)).toList();
}

Doesn't it make sense to have this method part of the library, so that you can pass in a Map or a List to fromJson?

It's just a lot of repeated code if you have a large api that returns JSONArrays for each of it's calls.

Agreed.

On Thu, Apr 12, 2018 at 7:52 AM Faisal Abid notifications@github.com
wrote:

Doesn't it make sense to have this method part of the library, so that you
can pass in a Map or a List to fromJson?

It's just a lot of repeated code if you have a large api that returns
JSONArrays for each of it's calls.

—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
https://github.com/dart-lang/json_serializable/issues/135#issuecomment-380833594,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AABCikf7QPSyNpywb85PKJS5AUZj94VZks5tn2oQgaJpZM4TRE2Q
.

to add to that, does it also make sense to do a json.decode(). why not just pass the json string and let json_serializeable deal with it?

for example

MyClass c = new MyClass.fromJson(responsePayloadFromServer);

Neat and clean

You can serialize as follows:

List<Obj> (jsonDecode(value) as List).map((f) => Obj.fromJson(f)).toList();

I hope I helped because it seems that the serializations shown are out of date.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hatjs880328s picture hatjs880328s  Â·  3Comments

4BooM04 picture 4BooM04  Â·  3Comments

enyo picture enyo  Â·  6Comments

emiliodallatorre picture emiliodallatorre  Â·  3Comments

xiang23 picture xiang23  Â·  5Comments