json to class dart

//insideclass
ClassName.fromJson(Map<String, dynamic> json) {
    variable1 = json['variable11'];
    variable2 = json['variable12'];
    variable3 = json['variable13']['variable14'];
}

//uses
//after http request or whatever
Map<dynamic, dynamic> res = await jsonDecode(response.body.toString());
Classname.fromJson(res);

3.63
8
Jeloh Simo 80 points

                                    The Data:
For this example, let's assume that my data looks something like this:
{     
    &quot;type&quot;: &quot;articles&quot;,     
    &quot;id&quot;: &quot;1&quot;,    
    &quot;data&quot;: [
        &quot;val&quot;,
        &quot;val2&quot;
    ],
    &quot;author&quot;: {
        &quot;id&quot;: &quot;42&quot;, 
        &quot;name&quot;: &quot;bo&quot;
    }   
}
This data object is rather simple but will help us highlight all the important cases that we care about.
Our Classes:
In this example, we have 2 classes that we are working with: Document.dart
class Document {
    final String type;
    final int id;
    final List&lt;String&gt; data;
    final Author author;
    Document({this.type, this.id, this.data, this.author});
    // add fromJson method here
}
and our Author.dart
class Author{
    final int id;
    final String name;
    Author({this.id, this.name});
    // add fromJson method here
}
FromJson Methods:
Let&rsquo;s first start with our Author class as it has no nested class objects in it.
class Author{
    final int id;
    final String name;
    Author({this.id, this.name});
    factory Author.fromJson(Map&lt;String, dynamic&gt; json) {
        return Author(
            id: json[&quot;id&quot;],
            name: json[&quot;name&quot;],
        );
    }
}
As you can see, it&rsquo;s a rather simple method when you are dealing with just converting simple variables from JSON into your class objects. To ensure we don&rsquo;t have any issues with the format in which the data is received from our APIs, you can go a step further and cast the JSON values into your object type like so:
json[&quot;name&quot;].toString()
This is not necessary, but sometimes useful if the source of the API is not reliable or a professional service. I&rsquo;ve seen instances in the past where when a double is equal to 0, the JSON object reads it as an int instead of a double which then throws an error. Casting your value into a variable type is a safe way of handling those rare cases.
Next, let&rsquo;s take a look at our parent class, Document:
class Document {
    final String type;
    final int id;
    final List&lt;String&gt; data;
    final Author author;
    Document({this.type, this.id, this.data, this.author});
factory Document.fromJson(Map&lt;String, dynamic&gt; json) {
        var dataObj = json['data'];
        return Document(
            type: json[&quot;type&quot;],
            id: json[&quot;id&quot;],
            data: new List&lt;String&gt;.from(dataObj),
            author: Author.fromJson(json['author']),
        );
    }
}

3.63 (8 Votes)
0
3
1

                                    class Autogenerated {
  int userId;
  int id;
  String title;
  String body;

  Autogenerated({this.userId, this.id, this.title, this.body});

  Autogenerated.fromJson(Map&lt;String, dynamic&gt; json) {
    userId = json['userId'];
    id = json['id'];
    title = json['title'];
    body = json['body'];
  }

  Map&lt;String, dynamic&gt; toJson() {
    final Map&lt;String, dynamic&gt; data = new Map&lt;String, dynamic&gt;();
    data['userId'] = this.userId;
    data['id'] = this.id;
    data['title'] = this.title;
    data['body'] = this.body;
    return data;
  }
}

3 (1 Votes)
0
0
0

                                    class BusinessProfileEditModel {
  String name;
  String address;
  String email;
  String phoneNumber;
  String rcNumber;
  String businessRegistrationTypeKey;
  String businessVerticalKey;
  String countryKey;
  String lgaKey;

  BusinessProfileEditModel(
      {this.name,
      this.address,
      this.email,
      this.phoneNumber,
      this.rcNumber,
      this.businessRegistrationTypeKey,
      this.businessVerticalKey,
      this.countryKey,
      this.lgaKey});

  BusinessProfileEditModel.fromJson(Map&lt;String, dynamic&gt; json) {
    name = json['name'];
    address = json['address'];
    email = json['email'];
    phoneNumber = json['phoneNumber'];
    rcNumber = json['rcNumber'];
    businessRegistrationTypeKey = json['businessRegistrationTypeKey'];
    businessVerticalKey = json['businessVerticalKey'];
    countryKey = json['countryKey'];
    lgaKey = json['lgaKey'];
  }

  Map&lt;String, dynamic&gt; toJson() {
    final Map&lt;String, dynamic&gt; data = new Map&lt;String, dynamic&gt;();
    data['name'] = this.name;
    data['address'] = this.address;
    data['email'] = this.email;
    data['phoneNumber'] = this.phoneNumber;
    data['rcNumber'] = this.rcNumber;
    data['businessRegistrationTypeKey'] = this.businessRegistrationTypeKey;
    data['businessVerticalKey'] = this.businessVerticalKey;
    data['countryKey'] = this.countryKey;
    data['lgaKey'] = this.lgaKey;
    return data;
  }
}

0
0
0
1
Joh Raul 85 points

                                    class Todo {
  int userId;
  int id;
  String title;
  bool completed;

  Todo({this.userId, this.id, this.title, this.completed});

  Todo.fromJson(Map&lt;String, dynamic&gt; json) {
    userId = json['userId'];
    id = json['id'];
    title = json['title'];
    completed = json['completed'];
  }

  Map&lt;String, dynamic&gt; toJson() {
    final Map&lt;String, dynamic&gt; data = new Map&lt;String, dynamic&gt;();
    data['userId'] = this.userId;
    data['id'] = this.id;
    data['title'] = this.title;
    data['completed'] = this.completed;
    return data;
  }
}

0
0
4.22
9

                                    class Autogenerated {
  int postId;
  int id;
  String name;
  String email;
  String body;

  Autogenerated({this.postId, this.id, this.name, this.email, this.body});

  Autogenerated.fromJson(Map&lt;String, dynamic&gt; json) {
    postId = json['postId'];
    id = json['id'];
    name = json['name'];
    email = json['email'];
    body = json['body'];
  }

  Map&lt;String, dynamic&gt; toJson() {
    final Map&lt;String, dynamic&gt; data = new Map&lt;String, dynamic&gt;();
    data['postId'] = this.postId;
    data['id'] = this.id;
    data['name'] = this.name;
    data['email'] = this.email;
    data['body'] = this.body;
    return data;
  }
}

4.22 (9 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
class to json object dart dart convert json to ini JSON to model class for flutter flutter json to dart class flutter convert json string to object dart json to classes dart json class object json model class in dart covert json to dart dart convert from json convert dart from json object to json in dart json to object dart dart converting json sream in to object dart converting json String in to object convert json to flutter model json string to model class in flutter convert complex json to dart json tot dart dart convert json string to json JSON to Dart Model Flutter convrt json to dart json convert model class in dart json to model class in dart json string to class in dart online json to dart model transform json to class flutter flutter dart convert json to array json to dart converter online how convert json dart to json dart convert an object to json convert json to flutter class online convert json to flutter class dart convert to json convert dart object to json flutter convert json object dart to json model to json in dart how to convert json to object flutter converting object to json object into flutter flutter model class to json flutter json to model class encoded to json dart online json string to dart converter json to dart example how to make json data in dart json to array converter dart json object to class object flutter convert json into object dart flutter json covert create a class to return a json dart create class from json dart online convert json to dart class json to dart list how to convert json to object in dart convert json object to java flutter create class in flutter from json JSON O DART javerbrick json to dart object type to json flutter json to to Dart data class convert a string to json object dart Convert to json flutter convert json string in to json object in flutter jsontoclass dart dart generate class from json converting string to json dart convert json to class flutter online how to create json objects in dart json transform dart dart convert json to map quick convert json to dart flutter json as class dart comvert object to json dart convert object to json convert object to json on dart dart create a json object dart from json flutter create object from json json into dart covert json into dart to string in dart from json json to class flutter dart code covert to json create json in dart convert dart class to json online convert json to dart class online convert class to json dart json to dart model\ Convert JSON to Dart class json object to model class dart json to dart pl flutter dart to json online converter dart class json flutter convert dart model into json dart json class how to create jsonObject in dart json object to object dart convert model to json dart dart json to model convert flutter create class from json dart to json online * make a json in dart convert javascript object to json in dart how to convert json in model calss in flutter' dart to json online json converter for dart jjson to dart convert string to json dart convert json to dart model dart object from json model to json string dart dart convert class to json json from object dart dart to jason cast json flutter convert json to a class flutter convert a flutter class to json json to clas dart json to dart how to turn json data into object flutter to json from json dart json tod dart how to convert dart class to json class dart format data to json create json dart json object to flutter object convert data to json obect flutter convert json to dart object convert jason to dart flutter string json to object json.encode dart:convert dart plain json object dart:convert string to json create json object dart convert json string to map dart json to dart class converter dart convert object to json string dart map json to object dart convert json string to object flutter json to dart convert json to map dart how to convert json string to json object in dart convert json to model dart json dart converter class package json dart converter class json dart converter json dart class dart convert json to class string to json in dart json to dart statefull make json in dart jsont to dart converters how to convert json decode in dart how to convert jsondecode in dart dart to json converters create a json object dart flutter create json object dart class to json string dart string to json online json to dart converter with nulsafety online json to dart converter json object dart convert json string to object flutter convert json to string dart make json object in flutter to json dart generate flutter class from json convert json to model for flutter flutter convert to json flutter json to class converter how to convert json data in dartpad how to convert json data in dart dartpad dart json to data class dart convert json to object dart convert json string to map to json flutter dart json converter json to model dart json to dart model online json to dart class online convert json to object flutter from dart to json jsonto dart create an json object in dart flutter dart string to json dart convert string to json flutter convert class to json dart flutter object from json flutter object to json flutter json to object json to dart online json dart json to dart code converter dart json convert convert json data to model class in flutter json to object flutter dart object to json string dart json string to object dart json to class dart class to json convert to json dart json to flutter class dart parse json to class convert dart to json online convert dart to json convert json to dart online convert json to model flutter dart to json data converter dart to json data how to convert json into dart classes flutter convert object to json generate dart class from json convert object to json flutter convert dart clasee object to json object json to dart in flutter object to json flutter parse json object to class object flutter flutter converting to jsonobject field flutter json to class convert json to class flutter type json to dart dart object to json converter dart object to json json to object flutter dart json to dart model converter json converter dart json to class dart dart convert json to model json convert to json dart object to json dart json to dart model how to convert json to dart in flutter jason to dart online json to dart how to convert json to dart json to dart dart example convert json to a model dart dart json to object how to create json in dart json convert dart json to dart online converter json string to json object dart dart to json converter string to json dart json to dart class online convert json to dart class convert json to dart dart json to model json in dart dart convert json object from json dart json to dart object json to dart converter json to dart flutter dart to json json string to model dart from json dart json convert to dart jsont to dart json to podo converter json to dart dart json parse generator
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source