1
votes

I search google and stackoverflow but did not find some auto converting lib like in C#

I need something in dart-flutter alternative to this code in C#

    string jsonTokenData = "{my token json data}";
    TokenModel getTokenModel = Newtonsoft.Json.JsonConvert.DeserializeObject<GetTokenModel>(jsonTokenData);

Update

  • I read the Documentation and I know how to map models with json
  • this question to who know how Newtonsoft lib work on C#
  • if you do not know Newtonsoft: the Idea is to convert json data to a model automatically without writing map for each model. Also I know there is a tool to create that map with json for each model automatically but still make code ridiculous.

Update2: as I get a suggestion from community for a similar question that not answer my question. so this is a another explanation:

I have a lot of models these models are updated by time as clients requested or adding new features to some models. so when an update happen I just need to add the extra properties that has been added to these models, I do not need to warry every time about mapping, spell mistaken or using some tools again to regenerate these codes.

so I'm asking for a function that take two parameters

  • first one is the type of the model
  • second one the json string that hold the data

[then in return is an object instance of the passed type]

for simple example if I have this class:

class Car {
    String name;
    String type;
}

Then I could keep it clean this way without getting it dart with other methods:

Car({
        this.name,
        this.type,
    });

    factory Car.fromJson(Map<String, dynamic> json) => Car(
        name: json["name"],
        type: json["type"],
    );

    Map<String, dynamic> toJson() => {
        "name": name,
        "type": type,
    };

by the way lib in C# is also take care about arrays, lists and nested classes

I hope this time it explained well

2
Could you address my comment on my answer? I already understand what your C# method is doing, but I would like to know how and what the issue with the method I proposed is. - Christopher Moore
it is a Lib for C# .net that automatically Create instance and retrieve it's properties of the a given type <GetTokenModel> then fetch data from json string and return that instance with the fetched data - Abdu Imam
And how does it do this automatically? What if the properties contained in the JSON don't match up with the fields of the object? How is this result different from what my answer would achieve? Please see my answer. - Christopher Moore
I think I have to do my own lib if now one have the answer by using import 'dart:mirrors'; it should do the job I will try with it - Abdu Imam
" it's return null values for not matching keys" and what does Newtonsoft do when there aren't matching fields? I'm trying to understand the behavior of Newtonsoft to find an alternative in dart. If you don't explain to me in detail what the exact behavior you want, it's very difficult to help without you rejecting any solution I offer. If you're simply asking for a library that will do the equivalent the simple answer is: it likely doesn't exist. - Christopher Moore

2 Answers

0
votes

just import dart:convert library like this :

 import 'dart:convert';

Then use this code :

json.decode(json)

Take a look at this link : dart:convert library

0
votes

Doing some research would allow you to not ask here on SO.

JSON decoding is well documented on the flutter site. It points to using the jsonDecode function, which parses the String and returns the resulting JSON object.

String jsonTokenData = "{my token json data}";
dynamic json = jsonDecode(jsonTokenData);

You can pass the decoded object to a custom constructor of your object like .fromJson, which accepts a Map/List depending on your JSON data.

If for some reason creating a constructor to accept JSON data would be too much work/typing or if you're having trouble doing it yourself, you can use a JSON to Dart service like this.