Home > Net >  How to create the correct json file for mobile app project
How to create the correct json file for mobile app project

Time:07-16

I'm not too familiar with JSON and I'm trying to create local JSON file for my Flutter Mobile app Project.

The way I plan to design my mobile app is, I will be displaying A list of Genre in my home page. When a user click a specific genre then it'll take to a new screen where it'll show all the song list that fall under those genre.

also I'll have another screen call "All songs" and it'll display all song with id order.

I have created this basic JSON file based on my knowledge and I'll be really appreciated if someone can review If this will work or I might need to modify it.

Any help or suggestion would be really appreciated.

[
    {
        "id": 0,
        "pageNumber": 1,
        "songNumber": "1",
        "title": "Acadian Driftwood",
        "genre": "Jazz",
        "favorite": false
    },
    {
        "id": 1,
        "pageNumber": 2,
        "songNumber": "2",
        "title": "Alfie",
        "genre": "Musical/Film",
        "favorite": false
    },
    {
        "id": 2,
        "pageNumber": 3,
        "songNumber": "3",
        "title": "Ace Of Spades",
        "genre": "Rock/Pop",
        "favorite": true
    },
    {
        "id": 3,
        "pageNumber": 4,
        "songNumber": "4",
        "title": "Alison",
        "genre": "Rock/Pop",
        "favorite": false
    },
    {
        "id": 4,
        "pageNumber": 5,
        "songNumber": "5",
        "title": "Alabamy Bound",
        "genre": "Musical/Film",
        "favorite": true
    }
]

CodePudding user response:

you can use Model to help you in this situation, then you can create a list of models and pass it to the next page

below is a model you can use in your app:

import 'dart:convert';

Song songFromJson(String str) => Song.fromJson(json.decode(str));

String songToJson(Song data) => json.encode(data.toJson());

class Song {
Song({
    this.id,
    this.pageNumber,
    this.songNumber,
    this.title,
    this.genre,
    this.favorite,
});

int id;
int pageNumber;
String songNumber;
String title;
String genre;
bool favorite;

factory Song.fromJson(Map<String, dynamic> json) => Song(
    id: json["id"],
    pageNumber: json["pageNumber"],
    songNumber: json["songNumber"],
    title: json["title"],
    genre: json["genre"],
    favorite: json["favorite"],
);

Map<String, dynamic> toJson() => {
    "id": id,
    "pageNumber": pageNumber,
    "songNumber": songNumber,
    "title": title,
    "genre": genre,
    "favorite": favorite,
};

}

CodePudding user response:

Use this website it is best to create a file directly from the JSON response or you can use the plugin in android studio jsonToDart

CodePudding user response:

 {

"data":[
{
    "id": 0,
    "pageNumber": 1,
    "songNumber": "1",
    "title": "Acadian Driftwood",
    "genre": "Jazz",
    "favorite": false
},
{
    "id": 1,
    "pageNumber": 2,
    "songNumber": "2",
    "title": "Alfie",
    "genre": "Musical/Film",
    "favorite": false
},
{
    "id": 2,
    "pageNumber": 3,
    "songNumber": "3",
    "title": "Ace Of Spades",
    "genre": "Rock/Pop",
    "favorite": true
},
{
    "id": 3,
    "pageNumber": 4,
    "songNumber": "4",
    "title": "Alison",
    "genre": "Rock/Pop",
    "favorite": false
},
{
    "id": 4,
    "pageNumber": 5,
    "songNumber": "5",
    "title": "Alabamy Bound",
    "genre": "Musical/Film",
    "favorite": true
}
 ]
}

that is the correct way of creating json file

  • Related