Home > Mobile >  How to decode json array in dart/flutter?
How to decode json array in dart/flutter?

Time:11-17

When i send request to server i get a json like this:

[
    {
        "id": 56012,
        "name": "Thename",
        "slug": "anything",
        "permalink": "my link",
        "date_created": "2021-11-06T09:55:58",
        "date_created_gmt": "2021-11-06T06:25:58",
        "date_modified": "2021-11-16T16:32:57",
        "date_modified_gmt": "2021-11-16T13:02:57",
        "type": "simple",
        "status": "publish",
        "featured": false,
        "catalog_visibility": "visible",
        "description": "<p>some thing</p>\n",
        "short_description": "",
        "sku": "6260492610086",
        "price": "45000",
        "regular_price": "45000",
        "sale_price": "45000",
        "weight": "",
        "dimensions": {
            "length": "",
            "width": "",
            "height": ""
        },
        "categories": [
            {
                "id": 1865,
                "name": "name",
                "slug": "123"
            }
        ],
        "tags": [],
        "images": [
            {
                "id": 56043,
                "src": "another link",
                "name": "Name",
                "alt": ""
            }
        ],
        }
    },
]

I want to know how can i decode this json in dart? i can decode and use "name" and "id" but my problem is image. i don`t know how to use "src" which is in image.

CodePudding user response:

The simple way

var dataList = [
    {
        "id": 56012,
        "name": "Thename",
        "slug": "anything",
        "permalink": "my link",
        "date_created": "2021-11-06T09:55:58",
        "date_created_gmt": "2021-11-06T06:25:58",
        "date_modified": "2021-11-16T16:32:57",
        "date_modified_gmt": "2021-11-16T13:02:57",
        "type": "simple",
        "status": "publish",
        "featured": false,
        "catalog_visibility": "visible",
        "description": "<p>some thing</p>\n",
        "short_description": "",
        "sku": "6260492610086",
        "price": "45000",
        "regular_price": "45000",
        "sale_price": "45000",
        "weight": "",
        "dimensions": {
            "length": "",
            "width": "",
            "height": ""
        },
        "categories": [
            {
                "id": 1865,
                "name": "name",
                "slug": "123"
            }
        ],
        "tags": [],
        "images": [
            {
                "id": 56043,
                "src": "another link",
                "name": "Name",
                "alt": ""
            }
        ],
        }
    },
];

var src = dataList[0]['images'][0]['src'];

but I recommend like this

var dataList =[
    {
        "id": 1,
        "name": "sample1"
    },
    {
        "id": 2,
        "name": "sample2"
    }
];



---
class Data {
  int id;
  String name;

  Data({this.id, this.name});

  Data.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['name'] = this.name;
    return data;
  }
}
---

var dataObjectList = dataList.map((e)=>Data.fromJson(e)).toList();

var id = dataObjectList.id;

CodePudding user response:

recommend using quicktype.io, you will have to copy that json response and it will convert it to Dart code. it is very easy to use

CodePudding user response:

Try below answer hope its helpful to you.

  • refer JSON and serialization here
  • refer dart:convert library here
  • refer Parse JSON here
  • you used jsonDecode function here

Use import 'dart:convert' package

json.decode(yourjsonString_varibale) 

CodePudding user response:

You can decode the code by:

var result = json.decode(jsonVariable)

then :

find your image object like

result['images'][[0]['src']

Use the src link in flutter network.image.

image object is a list so use ListView.builder.

  • Related