How to go on about Receiving JSON Array in Flutter and Parsing it?

· 3 min read
How to go on about receiving JSON array in flutter and parsing it
How to go on about receiving JSON array in flutter and parsing it

JSON is short for JavaScript Object Notation and is a way to store information in an organized, easy-to-access manner. generally, the entire communication between the app and the server is through JSON. So in this article, we will go through How to go on about Receiving JSON Array in Flutter and Parsing it.

How to go on about receiving JSON array in Flutter and parsing it?

You can do the following:

String receivedJson = "... Your JSON string ....";
List<dynamic> list = json.decode(receivedJson);
Fact fact = Fact.fromJson(list[0]);

In any case, you must consider the following in your JSON string and the Fact class that you have crafted.

  • In the JSON string, the id and fact_id are Strings and you treat them as int. Either you change the JSON or the Fact class
  • Some strings inside the JSON string produce errors as they have additional quotation marks and this confuses the decoder.

A JSON string the works are the following:

String receivedJson = """
[
    {
        "id": 407,
        "fact": "Monsanto once tried to genetically engineer blue cotton, to produce denim without the use of dyes, reducing the pollution involved in the dyeing process.   ",
        "reference": null,
        "image": "http:\/\/quickfacts.me\/wp-content\/uploads\/2015\/06\/fact492.png",
        "fact_id": 1
    }
]
    """;

With a simple array like the following:

{
  "tags": [
    "dart",
    "flutter",
    "json"
  ]
}

We can easily parse the JSON into a Dart array without the need of creating any class.

import 'dart:convert';

main() {
  String arrayText = '{"tags": ["dart", "flutter", "json"]}';

  var tagsJson = jsonDecode(arrayText)['tags'];
  List<String> tags = tagsJson != null ? List.from(tagsJson) : null;

  print(tags);
}

Now you can see the result of printing the Dart Array.

[dart, flutter, json]

Conclusion:

Thanks for being with us !!! Stay connected with us on this flutter journey!!

So in this article, we learned How to go on about Receiving JSON Array in Flutter and Parsing it. Lots of amazing content coming up your way !!!

Let us know if you are still facing any doubts in Flutter Development !! we would love to assist you 🙂

Keep Learning !!! Keep Fluttering !!! Stay tuned 🙂

Don’t forget to drop your valuable feedback/suggestion in the comments right below!!

Flutter Agency is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

Flutter Agency is one of the most popular online portals dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge of Flutter.

Leave a Reply