'auto converting json to object in dart flutter
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
Solution 1:[1]
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
Solution 2:[2]
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.
Solution 3:[3]
I've added a new Dart package to Jsonize custom classes within any data structure.
This is how your Car example would look like:
import 'package:jsonize/jsonize.dart';
class Car implements Jsonizable<Car> {
final String name;
final String type;
Car({required this.name, required this.type});
// Jsonizable implementation
factory Car.empty() => Car(name: "", type: "");
@override
String get jsonClassCode => "car";
@override
Map<String, dynamic> toJson() => {"name": name, "type": type};
@override
Car? fromJson(value) => Car(name: value["name"], type: value["type"]);
}
void main() {
// Register classes
Jsonize.registerClass(Car.empty());
// Now you can jsonize it wherever
Map<String, dynamic> myMap = {
"my_car": Car(name: "Jessie", type: "Tesla"),
"purchase_dt": DateTime.now(),
};
var jsonRep = Jsonize.toJson(myMap);
var hereIsMyMap = Jsonize.fromJson(jsonRep);
print(hereIsMyMap);
List<dynamic> myList = [
Car(name: "Jessie", type: "Tesla"),
Car(name: "Bob", type: "Ford"),
];
jsonRep = Jsonize.toJson(myList);
var hereIsMyList = Jsonize.fromJson(jsonRep);
print(hereIsMyList);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | farouk osama |
Solution 2 | |
Solution 3 | cabbi |