我想扑朔迷离地解析一个复杂的JSON,我已经尝试了很多事情,但是我不清楚事情的发生方式。谁能解释我如何处理这种情况:
{
"message": {
"header": {
"status_code": 200,
"execute_time": 0.021989107131958
},
"body": {
"track_list": [
{
"track": {
"track_id": 200357565,
"track_name": "The Climb Back",
"track_name_translation_list": [],
"track_rating": 99,
"commontrack_id": 113835591,
"instrumental": 0,
"explicit": 1,
"has_lyrics": 1,
"has_subtitles": 1,
"has_richsync": 1,
"num_favourite": 20,
"album_id": 39278073,
"album_name": "Lewis Street",
"artist_id": 486280,
"artist_name": "J. Cole",
"track_share_url": "https:\/\/www.musixmatch.com\/lyrics\/J-Cole\/The-Climb-Back?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"track_edit_url": "https:\/\/www.musixmatch.com\/lyrics\/J-Cole\/The-Climb-Back\/edit?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"restricted": 0,
"updated_time": "2020-07-23T14:02:17Z",
"primary_genres": {
"music_genre_list": []
}
}
},
{
"track": {
"track_id": 200187374,
"track_name": "POPSTAR (feat. Drake)",
"track_name_translation_list": [],
"track_rating": 99,
"commontrack_id": 113684105,
"instrumental": 0,
"explicit": 1,
"has_lyrics": 1,
"has_subtitles": 1,
"has_richsync": 1,
"num_favourite": 6,
"album_id": 39216808,
"album_name": "POPSTAR (feat. Drake) - Single",
"artist_id": 24704782,
"artist_name": "DJ Khaled feat. Drake, Lil Wayne & Rick Ross",
"track_share_url": "https:\/\/www.musixmatch.com\/lyrics\/DJ-Khaled-feat-Drake-Lil-Wayne-Rick-Ross\/POPSTAR-Drake?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"track_edit_url": "https:\/\/www.musixmatch.com\/lyrics\/DJ-Khaled-feat-Drake-Lil-Wayne-Rick-Ross\/POPSTAR-Drake\/edit?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"restricted": 0,
"updated_time": "2020-07-17T13:38:30Z",
"primary_genres": {
"music_genre_list": []
}
}
},
{
"track": {
"track_id": 200102123,
"track_name": "What's Love Got to Do with It",
"track_name_translation_list": [],
"track_rating": 99,
"commontrack_id": 113606423,
"instrumental": 0,
"explicit": 0,
"has_lyrics": 1,
"has_subtitles": 1,
"has_richsync": 1,
"num_favourite": 18,
"album_id": 39186667,
"album_name": "What's Love Got to Do with It",
"artist_id": 45989671,
"artist_name": "Kygo feat. Tina Turner",
"track_share_url": "https:\/\/www.musixmatch.com\/lyrics\/Kygo-Tina-Turner\/What-s-Love-Got-to-Do-with-It?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"track_edit_url": "https:\/\/www.musixmatch.com\/lyrics\/Kygo-Tina-Turner\/What-s-Love-Got-to-Do-with-It\/edit?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"restricted": 0,
"updated_time": "2020-07-23T06:28:49Z",
"primary_genres": {
"music_genre_list": []
}
}
},
{
"track": {
"track_id": 194169151,
"track_name": "Blinding Lights",
"track_name_translation_list": [],
"track_rating": 100,
"commontrack_id": 104185748,
"instrumental": 0,
"explicit": 0,
"has_lyrics": 1,
"has_subtitles": 1,
"has_richsync": 1,
"num_favourite": 5583,
"album_id": 37216011,
"album_name": "After Hours",
"artist_id": 13937035,
"artist_name": "The Weeknd",
"track_share_url": "https:\/\/www.musixmatch.com\/lyrics\/The-Weeknd-3\/Blinding-Lights?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"track_edit_url": "https:\/\/www.musixmatch.com\/lyrics\/The-Weeknd-3\/Blinding-Lights\/edit?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"restricted": 0,
"updated_time": "2020-07-09T11:24:05Z",
"primary_genres": {
"music_genre_list": [
{
"music_genre": {
"music_genre_id": 7,
"music_genre_parent_id": 34,
"music_genre_name": "Electronic",
"music_genre_name_extended": "Electronic",
"music_genre_vanity": "Electronic"
}
}
]
}
}
},
{
"track": {
"track_id": 198421234,
"track_name": "Savage Love (Laxed - Siren Beat)",
"track_name_translation_list": [],
"track_rating": 100,
"commontrack_id": 112136372,
"instrumental": 0,
"explicit": 1,
"has_lyrics": 1,
"has_subtitles": 1,
"has_richsync": 1,
"num_favourite": 454,
"album_id": 38617941,
"album_name": "Savage Love (Laxed - Siren Beat) - Single",
"artist_id": 45643815,
"artist_name": "Jawsh 685 feat. Jason Derulo",
"track_share_url": "https:\/\/www.musixmatch.com\/lyrics\/Jawsh-685-Jason-Derulo\/Savage-Love-Laxed-Siren-Beat?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"track_edit_url": "https:\/\/www.musixmatch.com\/lyrics\/Jawsh-685-Jason-Derulo\/Savage-Love-Laxed-Siren-Beat\/edit?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"restricted": 0,
"updated_time": "2020-07-09T13:06:43Z",
"primary_genres": {
"music_genre_list": []
}
}
},
{
"track": {
"track_id": 189626475,
"track_name": "Watermelon Sugar",
"track_name_translation_list": [],
"track_rating": 100,
"commontrack_id": 104939195,
"instrumental": 0,
"explicit": 0,
"has_lyrics": 1,
"has_subtitles": 1,
"has_richsync": 1,
"num_favourite": 567,
"album_id": 36101498,
"album_name": "Fine Line",
"artist_id": 24505463,
"artist_name": "Harry Styles",
"track_share_url": "https:\/\/www.musixmatch.com\/lyrics\/Harry-Styles\/Watermelon-Sugar-2?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"track_edit_url": "https:\/\/www.musixmatch.com\/lyrics\/Harry-Styles\/Watermelon-Sugar-2\/edit?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"restricted": 0,
"updated_time": "2019-12-12T16:57:16Z",
"primary_genres": {
"music_genre_list": []
}
}
},
{
"track": {
"track_id": 192324206,
"track_name": "One Margarita",
"track_name_translation_list": [],
"track_rating": 99,
"commontrack_id": 107705631,
"instrumental": 0,
"explicit": 0,
"has_lyrics": 1,
"has_subtitles": 1,
"has_richsync": 1,
"num_favourite": 62,
"album_id": 36710192,
"album_name": "Born Here Live Here Die Here",
"artist_id": 360257,
"artist_name": "Luke Bryan",
"track_share_url": "https:\/\/www.musixmatch.com\/lyrics\/Luke-Bryan\/One-Margarita?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"track_edit_url": "https:\/\/www.musixmatch.com\/lyrics\/Luke-Bryan\/One-Margarita\/edit?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"restricted": 0,
"updated_time": "2020-03-13T08:54:41Z",
"primary_genres": {
"music_genre_list": [
{
"music_genre": {
"music_genre_id": 34,
"music_genre_parent_id": 0,
"music_genre_name": "Music",
"music_genre_name_extended": "Music",
"music_genre_vanity": "Music"
}
}
]
}
}
},
{
"track": {
"track_id": 195445553,
"track_name": "ROCKSTAR (feat. Roddy Ricch)",
"track_name_translation_list": [],
"track_rating": 99,
"commontrack_id": 109586143,
"instrumental": 0,
"explicit": 1,
"has_lyrics": 1,
"has_subtitles": 1,
"has_richsync": 1,
"num_favourite": 687,
"album_id": 37641727,
"album_name": "BLAME IT ON BABY",
"artist_id": 45035319,
"artist_name": "DaBaby feat. Roddy Ricch",
"track_share_url": "https:\/\/www.musixmatch.com\/lyrics\/DaBaby-Roddy-Ricch\/ROCKSTAR-Roddy-Ricch?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"track_edit_url": "https:\/\/www.musixmatch.com\/lyrics\/DaBaby-Roddy-Ricch\/ROCKSTAR-Roddy-Ricch\/edit?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"restricted": 0,
"updated_time": "2020-06-22T12:43:00Z",
"primary_genres": {
"music_genre_list": []
}
}
},
{
"track": {
"track_id": 200340862,
"track_name": "Patience",
"track_name_translation_list": [],
"track_rating": 98,
"commontrack_id": 113824017,
"instrumental": 0,
"explicit": 0,
"has_lyrics": 1,
"has_subtitles": 1,
"has_richsync": 0,
"num_favourite": 4,
"album_id": 39273295,
"album_name": "Patience",
"artist_id": 156,
"artist_name": "Chris Cornell",
"track_share_url": "https:\/\/www.musixmatch.com\/lyrics\/Chris-Cornell\/Patience?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"track_edit_url": "https:\/\/www.musixmatch.com\/lyrics\/Chris-Cornell\/Patience\/edit?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"restricted": 0,
"updated_time": "2020-07-21T20:15:22Z",
"primary_genres": {
"music_genre_list": []
}
}
},
{
"track": {
"track_id": 200360270,
"track_name": "Say Something",
"track_name_translation_list": [],
"track_rating": 1,
"commontrack_id": 113837622,
"instrumental": 0,
"explicit": 0,
"has_lyrics": 1,
"has_subtitles": 1,
"has_richsync": 0,
"num_favourite": 0,
"album_id": 39278673,
"album_name": "DISCO (Deluxe)",
"artist_id": 7712,
"artist_name": "Kylie Minogue",
"track_share_url": "https:\/\/www.musixmatch.com\/lyrics\/Kylie-Minogue\/Say-Something?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"track_edit_url": "https:\/\/www.musixmatch.com\/lyrics\/Kylie-Minogue\/Say-Something\/edit?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"restricted": 0,
"updated_time": "2020-07-23T16:04:00Z",
"primary_genres": {
"music_genre_list": [
{
"music_genre": {
"music_genre_id": 34,
"music_genre_parent_id": 0,
"music_genre_name": "Music",
"music_genre_name_extended": "Music",
"music_genre_vanity": "Music"
}
}
]
}
}
}
]
}
}
}
我用https://app.quicktype.io/得到了这样的东西:
// To parse this JSON data, do
//
// final jsonToDart = jsonToDartFromMap(jsonString);
import 'dart:convert';
JsonToDart jsonToDartFromMap(String str) => JsonToDart.fromMap(json.decode(str));
String jsonToDartToMap(JsonToDart data) => json.encode(data.toMap());
class JsonToDart {
JsonToDart({
this.message,
});
Message message;
factory JsonToDart.fromMap(Map<String, dynamic> json) => JsonToDart(
message: Message.fromMap(json["message"]),
);
Map<String, dynamic> toMap() => {
"message": message.toMap(),
};
}
class Message {
Message({
this.header,
this.body,
});
Header header;
Body body;
factory Message.fromMap(Map<String, dynamic> json) => Message(
header: Header.fromMap(json["header"]),
body: Body.fromMap(json["body"]),
);
Map<String, dynamic> toMap() => {
"header": header.toMap(),
"body": body.toMap(),
};
}
class Body {
Body({
this.trackList,
});
List<TrackList> trackList;
factory Body.fromMap(Map<String, dynamic> json) => Body(
trackList: List<TrackList>.from(json["track_list"].map((x) => TrackList.fromMap(x))),
);
Map<String, dynamic> toMap() => {
"track_list": List<dynamic>.from(trackList.map((x) => x.toMap())),
};
}
class TrackList {
TrackList({
this.track,
});
Track track;
factory TrackList.fromMap(Map<String, dynamic> json) => TrackList(
track: Track.fromMap(json["track"]),
);
Map<String, dynamic> toMap() => {
"track": track.toMap(),
};
}
class Track {
Track({
this.trackId,
this.trackName,
this.trackNameTranslationList,
this.trackRating,
this.commontrackId,
this.instrumental,
this.explicit,
this.hasLyrics,
this.hasSubtitles,
this.hasRichsync,
this.numFavourite,
this.albumId,
this.albumName,
this.artistId,
this.artistName,
this.trackShareUrl,
this.trackEditUrl,
this.restricted,
this.updatedTime,
this.primaryGenres,
});
int trackId;
String trackName;
List<dynamic> trackNameTranslationList;
int trackRating;
int commontrackId;
int instrumental;
int explicit;
int hasLyrics;
int hasSubtitles;
int hasRichsync;
int numFavourite;
int albumId;
String albumName;
int artistId;
String artistName;
String trackShareUrl;
String trackEditUrl;
int restricted;
DateTime updatedTime;
PrimaryGenres primaryGenres;
factory Track.fromMap(Map<String, dynamic> json) => Track(
trackId: json["track_id"],
trackName: json["track_name"],
trackNameTranslationList: List<dynamic>.from(json["track_name_translation_list"].map((x) => x)),
trackRating: json["track_rating"],
commontrackId: json["commontrack_id"],
instrumental: json["instrumental"],
explicit: json["explicit"],
hasLyrics: json["has_lyrics"],
hasSubtitles: json["has_subtitles"],
hasRichsync: json["has_richsync"],
numFavourite: json["num_favourite"],
albumId: json["album_id"],
albumName: json["album_name"],
artistId: json["artist_id"],
artistName: json["artist_name"],
trackShareUrl: json["track_share_url"],
trackEditUrl: json["track_edit_url"],
restricted: json["restricted"],
updatedTime: DateTime.parse(json["updated_time"]),
primaryGenres: PrimaryGenres.fromMap(json["primary_genres"]),
);
Map<String, dynamic> toMap() => {
"track_id": trackId,
"track_name": trackName,
"track_name_translation_list": List<dynamic>.from(trackNameTranslationList.map((x) => x)),
"track_rating": trackRating,
"commontrack_id": commontrackId,
"instrumental": instrumental,
"explicit": explicit,
"has_lyrics": hasLyrics,
"has_subtitles": hasSubtitles,
"has_richsync": hasRichsync,
"num_favourite": numFavourite,
"album_id": albumId,
"album_name": albumName,
"artist_id": artistId,
"artist_name": artistName,
"track_share_url": trackShareUrl,
"track_edit_url": trackEditUrl,
"restricted": restricted,
"updated_time": updatedTime.toIso8601String(),
"primary_genres": primaryGenres.toMap(),
};
}
class PrimaryGenres {
PrimaryGenres({
this.musicGenreList,
});
List<MusicGenreList> musicGenreList;
factory PrimaryGenres.fromMap(Map<String, dynamic> json) => PrimaryGenres(
musicGenreList: List<MusicGenreList>.from(json["music_genre_list"].map((x) => MusicGenreList.fromMap(x))),
);
Map<String, dynamic> toMap() => {
"music_genre_list": List<dynamic>.from(musicGenreList.map((x) => x.toMap())),
};
}
class MusicGenreList {
MusicGenreList({
this.musicGenre,
});
MusicGenre musicGenre;
factory MusicGenreList.fromMap(Map<String, dynamic> json) => MusicGenreList(
musicGenre: MusicGenre.fromMap(json["music_genre"]),
);
Map<String, dynamic> toMap() => {
"music_genre": musicGenre.toMap(),
};
}
class MusicGenre {
MusicGenre({
this.musicGenreId,
this.musicGenreParentId,
this.musicGenreName,
this.musicGenreNameExtended,
this.musicGenreVanity,
});
int musicGenreId;
int musicGenreParentId;
String musicGenreName;
String musicGenreNameExtended;
String musicGenreVanity;
factory MusicGenre.fromMap(Map<String, dynamic> json) => MusicGenre(
musicGenreId: json["music_genre_id"],
musicGenreParentId: json["music_genre_parent_id"],
musicGenreName: json["music_genre_name"],
musicGenreNameExtended: json["music_genre_name_extended"],
musicGenreVanity: json["music_genre_vanity"],
);
Map<String, dynamic> toMap() => {
"music_genre_id": musicGenreId,
"music_genre_parent_id": musicGenreParentId,
"music_genre_name": musicGenreName,
"music_genre_name_extended": musicGenreNameExtended,
"music_genre_vanity": musicGenreVanity,
};
}
class Header {
Header({
this.statusCode,
this.executeTime,
});
int statusCode;
double executeTime;
factory Header.fromMap(Map<String, dynamic> json) => Header(
statusCode: json["status_code"],
executeTime: json["execute_time"].toDouble(),
);
Map<String, dynamic> toMap() => {
"status_code": statusCode,
"execute_time": executeTime,
};
}
我在主要功能中使用了它:
Future<JsonToDart> getData() async {
var response = await http.get(url1);
print("ssssssssssssssss");
print(response.body);
return jsonToDartFromMap(response.body);
}
但是我不知道如何从这里开始。我必须获取trackId,trackName,artistName,albumName的值并在listView中显示。许多教程具有不同的样式,并且这些JSON数据我只是不明白如何简化。请帮忙
答案 0 :(得分:1)
只需检查一下我创建的示例: 这是您提供的json
{
"message": {
"header": {
"status_code": 200,
"execute_time": 0.021989107131958
},
"body": {
"track_list": [
{
"track": {
"track_id": 200357565,
"track_name": "The Climb Back",
"track_name_translation_list": [],
"track_rating": 99,
"commontrack_id": 113835591,
"instrumental": 0,
"explicit": 1,
"has_lyrics": 1,
"has_subtitles": 1,
"has_richsync": 1,
"num_favourite": 20,
"album_id": 39278073,
"album_name": "Lewis Street",
"artist_id": 486280,
"artist_name": "J. Cole",
"track_share_url": "https:\/\/www.musixmatch.com\/lyrics\/J-Cole\/The-Climb-Back?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"track_edit_url": "https:\/\/www.musixmatch.com\/lyrics\/J-Cole\/The-Climb-Back\/edit?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"restricted": 0,
"updated_time": "2020-07-23T14:02:17Z",
"primary_genres": {
"music_genre_list": []
}
}
},
{
"track": {
"track_id": 200187374,
"track_name": "POPSTAR (feat. Drake)",
"track_name_translation_list": [],
"track_rating": 99,
"commontrack_id": 113684105,
"instrumental": 0,
"explicit": 1,
"has_lyrics": 1,
"has_subtitles": 1,
"has_richsync": 1,
"num_favourite": 6,
"album_id": 39216808,
"album_name": "POPSTAR (feat. Drake) - Single",
"artist_id": 24704782,
"artist_name": "DJ Khaled feat. Drake, Lil Wayne & Rick Ross",
"track_share_url": "https:\/\/www.musixmatch.com\/lyrics\/DJ-Khaled-feat-Drake-Lil-Wayne-Rick-Ross\/POPSTAR-Drake?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"track_edit_url": "https:\/\/www.musixmatch.com\/lyrics\/DJ-Khaled-feat-Drake-Lil-Wayne-Rick-Ross\/POPSTAR-Drake\/edit?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"restricted": 0,
"updated_time": "2020-07-17T13:38:30Z",
"primary_genres": {
"music_genre_list": []
}
}
},
{
"track": {
"track_id": 200102123,
"track_name": "What's Love Got to Do with It",
"track_name_translation_list": [],
"track_rating": 99,
"commontrack_id": 113606423,
"instrumental": 0,
"explicit": 0,
"has_lyrics": 1,
"has_subtitles": 1,
"has_richsync": 1,
"num_favourite": 18,
"album_id": 39186667,
"album_name": "What's Love Got to Do with It",
"artist_id": 45989671,
"artist_name": "Kygo feat. Tina Turner",
"track_share_url": "https:\/\/www.musixmatch.com\/lyrics\/Kygo-Tina-Turner\/What-s-Love-Got-to-Do-with-It?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"track_edit_url": "https:\/\/www.musixmatch.com\/lyrics\/Kygo-Tina-Turner\/What-s-Love-Got-to-Do-with-It\/edit?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"restricted": 0,
"updated_time": "2020-07-23T06:28:49Z",
"primary_genres": {
"music_genre_list": []
}
}
},
{
"track": {
"track_id": 194169151,
"track_name": "Blinding Lights",
"track_name_translation_list": [],
"track_rating": 100,
"commontrack_id": 104185748,
"instrumental": 0,
"explicit": 0,
"has_lyrics": 1,
"has_subtitles": 1,
"has_richsync": 1,
"num_favourite": 5583,
"album_id": 37216011,
"album_name": "After Hours",
"artist_id": 13937035,
"artist_name": "The Weeknd",
"track_share_url": "https:\/\/www.musixmatch.com\/lyrics\/The-Weeknd-3\/Blinding-Lights?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"track_edit_url": "https:\/\/www.musixmatch.com\/lyrics\/The-Weeknd-3\/Blinding-Lights\/edit?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"restricted": 0,
"updated_time": "2020-07-09T11:24:05Z",
"primary_genres": {
"music_genre_list": [
{
"music_genre": {
"music_genre_id": 7,
"music_genre_parent_id": 34,
"music_genre_name": "Electronic",
"music_genre_name_extended": "Electronic",
"music_genre_vanity": "Electronic"
}
}
]
}
}
},
{
"track": {
"track_id": 198421234,
"track_name": "Savage Love (Laxed - Siren Beat)",
"track_name_translation_list": [],
"track_rating": 100,
"commontrack_id": 112136372,
"instrumental": 0,
"explicit": 1,
"has_lyrics": 1,
"has_subtitles": 1,
"has_richsync": 1,
"num_favourite": 454,
"album_id": 38617941,
"album_name": "Savage Love (Laxed - Siren Beat) - Single",
"artist_id": 45643815,
"artist_name": "Jawsh 685 feat. Jason Derulo",
"track_share_url": "https:\/\/www.musixmatch.com\/lyrics\/Jawsh-685-Jason-Derulo\/Savage-Love-Laxed-Siren-Beat?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"track_edit_url": "https:\/\/www.musixmatch.com\/lyrics\/Jawsh-685-Jason-Derulo\/Savage-Love-Laxed-Siren-Beat\/edit?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"restricted": 0,
"updated_time": "2020-07-09T13:06:43Z",
"primary_genres": {
"music_genre_list": []
}
}
},
{
"track": {
"track_id": 189626475,
"track_name": "Watermelon Sugar",
"track_name_translation_list": [],
"track_rating": 100,
"commontrack_id": 104939195,
"instrumental": 0,
"explicit": 0,
"has_lyrics": 1,
"has_subtitles": 1,
"has_richsync": 1,
"num_favourite": 567,
"album_id": 36101498,
"album_name": "Fine Line",
"artist_id": 24505463,
"artist_name": "Harry Styles",
"track_share_url": "https:\/\/www.musixmatch.com\/lyrics\/Harry-Styles\/Watermelon-Sugar-2?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"track_edit_url": "https:\/\/www.musixmatch.com\/lyrics\/Harry-Styles\/Watermelon-Sugar-2\/edit?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"restricted": 0,
"updated_time": "2019-12-12T16:57:16Z",
"primary_genres": {
"music_genre_list": []
}
}
},
{
"track": {
"track_id": 192324206,
"track_name": "One Margarita",
"track_name_translation_list": [],
"track_rating": 99,
"commontrack_id": 107705631,
"instrumental": 0,
"explicit": 0,
"has_lyrics": 1,
"has_subtitles": 1,
"has_richsync": 1,
"num_favourite": 62,
"album_id": 36710192,
"album_name": "Born Here Live Here Die Here",
"artist_id": 360257,
"artist_name": "Luke Bryan",
"track_share_url": "https:\/\/www.musixmatch.com\/lyrics\/Luke-Bryan\/One-Margarita?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"track_edit_url": "https:\/\/www.musixmatch.com\/lyrics\/Luke-Bryan\/One-Margarita\/edit?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"restricted": 0,
"updated_time": "2020-03-13T08:54:41Z",
"primary_genres": {
"music_genre_list": [
{
"music_genre": {
"music_genre_id": 34,
"music_genre_parent_id": 0,
"music_genre_name": "Music",
"music_genre_name_extended": "Music",
"music_genre_vanity": "Music"
}
}
]
}
}
},
{
"track": {
"track_id": 195445553,
"track_name": "ROCKSTAR (feat. Roddy Ricch)",
"track_name_translation_list": [],
"track_rating": 99,
"commontrack_id": 109586143,
"instrumental": 0,
"explicit": 1,
"has_lyrics": 1,
"has_subtitles": 1,
"has_richsync": 1,
"num_favourite": 687,
"album_id": 37641727,
"album_name": "BLAME IT ON BABY",
"artist_id": 45035319,
"artist_name": "DaBaby feat. Roddy Ricch",
"track_share_url": "https:\/\/www.musixmatch.com\/lyrics\/DaBaby-Roddy-Ricch\/ROCKSTAR-Roddy-Ricch?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"track_edit_url": "https:\/\/www.musixmatch.com\/lyrics\/DaBaby-Roddy-Ricch\/ROCKSTAR-Roddy-Ricch\/edit?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"restricted": 0,
"updated_time": "2020-06-22T12:43:00Z",
"primary_genres": {
"music_genre_list": []
}
}
},
{
"track": {
"track_id": 200340862,
"track_name": "Patience",
"track_name_translation_list": [],
"track_rating": 98,
"commontrack_id": 113824017,
"instrumental": 0,
"explicit": 0,
"has_lyrics": 1,
"has_subtitles": 1,
"has_richsync": 0,
"num_favourite": 4,
"album_id": 39273295,
"album_name": "Patience",
"artist_id": 156,
"artist_name": "Chris Cornell",
"track_share_url": "https:\/\/www.musixmatch.com\/lyrics\/Chris-Cornell\/Patience?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"track_edit_url": "https:\/\/www.musixmatch.com\/lyrics\/Chris-Cornell\/Patience\/edit?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"restricted": 0,
"updated_time": "2020-07-21T20:15:22Z",
"primary_genres": {
"music_genre_list": []
}
}
},
{
"track": {
"track_id": 200360270,
"track_name": "Say Something",
"track_name_translation_list": [],
"track_rating": 1,
"commontrack_id": 113837622,
"instrumental": 0,
"explicit": 0,
"has_lyrics": 1,
"has_subtitles": 1,
"has_richsync": 0,
"num_favourite": 0,
"album_id": 39278673,
"album_name": "DISCO (Deluxe)",
"artist_id": 7712,
"artist_name": "Kylie Minogue",
"track_share_url": "https:\/\/www.musixmatch.com\/lyrics\/Kylie-Minogue\/Say-Something?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"track_edit_url": "https:\/\/www.musixmatch.com\/lyrics\/Kylie-Minogue\/Say-Something\/edit?utm_source=application&utm_campaign=api&utm_medium=n%2Fa%3A1409620463618",
"restricted": 0,
"updated_time": "2020-07-23T16:04:00Z",
"primary_genres": {
"music_genre_list": [
{
"music_genre": {
"music_genre_id": 34,
"music_genre_parent_id": 0,
"music_genre_name": "Music",
"music_genre_name_extended": "Music",
"music_genre_vanity": "Music"
}
}
]
}
}
}
]
}
}
}
这是json的模型:
// To parse this JSON data, do
//
// final track = trackFromJson(jsonString);
import 'dart:convert';
Track trackFromJson(String str) => Track.fromJson(json.decode(str));
String trackToJson(Track data) => json.encode(data.toJson());
class Track {
Track({
this.message,
});
Message message;
factory Track.fromJson(Map<String, dynamic> json) => Track(
message: Message.fromJson(json["message"]),
);
Map<String, dynamic> toJson() => {
"message": message.toJson(),
};
}
class Message {
Message({
this.header,
this.body,
});
Header header;
Body body;
factory Message.fromJson(Map<String, dynamic> json) => Message(
header: Header.fromJson(json["header"]),
body: Body.fromJson(json["body"]),
);
Map<String, dynamic> toJson() => {
"header": header.toJson(),
"body": body.toJson(),
};
}
class Body {
Body({
this.trackList,
});
List<TrackList> trackList;
factory Body.fromJson(Map<String, dynamic> json) => Body(
trackList: List<TrackList>.from(json["track_list"].map((x) => TrackList.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"track_list": List<dynamic>.from(trackList.map((x) => x.toJson())),
};
}
class TrackList {
TrackList({
this.track,
});
TrackClass track;
factory TrackList.fromJson(Map<String, dynamic> json) => TrackList(
track: TrackClass.fromJson(json["track"]),
);
Map<String, dynamic> toJson() => {
"track": track.toJson(),
};
}
class TrackClass {
TrackClass({
this.trackId,
this.trackName,
this.trackNameTranslationList,
this.trackRating,
this.commontrackId,
this.instrumental,
this.explicit,
this.hasLyrics,
this.hasSubtitles,
this.hasRichsync,
this.numFavourite,
this.albumId,
this.albumName,
this.artistId,
this.artistName,
this.trackShareUrl,
this.trackEditUrl,
this.restricted,
this.updatedTime,
this.primaryGenres,
});
int trackId;
String trackName;
List<dynamic> trackNameTranslationList;
int trackRating;
int commontrackId;
int instrumental;
int explicit;
int hasLyrics;
int hasSubtitles;
int hasRichsync;
int numFavourite;
int albumId;
String albumName;
int artistId;
String artistName;
String trackShareUrl;
String trackEditUrl;
int restricted;
DateTime updatedTime;
PrimaryGenres primaryGenres;
factory TrackClass.fromJson(Map<String, dynamic> json) => TrackClass(
trackId: json["track_id"],
trackName: json["track_name"],
trackNameTranslationList: List<dynamic>.from(json["track_name_translation_list"].map((x) => x)),
trackRating: json["track_rating"],
commontrackId: json["commontrack_id"],
instrumental: json["instrumental"],
explicit: json["explicit"],
hasLyrics: json["has_lyrics"],
hasSubtitles: json["has_subtitles"],
hasRichsync: json["has_richsync"],
numFavourite: json["num_favourite"],
albumId: json["album_id"],
albumName: json["album_name"],
artistId: json["artist_id"],
artistName: json["artist_name"],
trackShareUrl: json["track_share_url"],
trackEditUrl: json["track_edit_url"],
restricted: json["restricted"],
updatedTime: DateTime.parse(json["updated_time"]),
primaryGenres: PrimaryGenres.fromJson(json["primary_genres"]),
);
Map<String, dynamic> toJson() => {
"track_id": trackId,
"track_name": trackName,
"track_name_translation_list": List<dynamic>.from(trackNameTranslationList.map((x) => x)),
"track_rating": trackRating,
"commontrack_id": commontrackId,
"instrumental": instrumental,
"explicit": explicit,
"has_lyrics": hasLyrics,
"has_subtitles": hasSubtitles,
"has_richsync": hasRichsync,
"num_favourite": numFavourite,
"album_id": albumId,
"album_name": albumName,
"artist_id": artistId,
"artist_name": artistName,
"track_share_url": trackShareUrl,
"track_edit_url": trackEditUrl,
"restricted": restricted,
"updated_time": updatedTime.toIso8601String(),
"primary_genres": primaryGenres.toJson(),
};
}
class PrimaryGenres {
PrimaryGenres({
this.musicGenreList,
});
List<MusicGenreList> musicGenreList;
factory PrimaryGenres.fromJson(Map<String, dynamic> json) => PrimaryGenres(
musicGenreList: List<MusicGenreList>.from(json["music_genre_list"].map((x) => MusicGenreList.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"music_genre_list": List<dynamic>.from(musicGenreList.map((x) => x.toJson())),
};
}
class MusicGenreList {
MusicGenreList({
this.musicGenre,
});
MusicGenre musicGenre;
factory MusicGenreList.fromJson(Map<String, dynamic> json) => MusicGenreList(
musicGenre: MusicGenre.fromJson(json["music_genre"]),
);
Map<String, dynamic> toJson() => {
"music_genre": musicGenre.toJson(),
};
}
class MusicGenre {
MusicGenre({
this.musicGenreId,
this.musicGenreParentId,
this.musicGenreName,
this.musicGenreNameExtended,
this.musicGenreVanity,
});
int musicGenreId;
int musicGenreParentId;
String musicGenreName;
String musicGenreNameExtended;
String musicGenreVanity;
factory MusicGenre.fromJson(Map<String, dynamic> json) => MusicGenre(
musicGenreId: json["music_genre_id"],
musicGenreParentId: json["music_genre_parent_id"],
musicGenreName: json["music_genre_name"],
musicGenreNameExtended: json["music_genre_name_extended"],
musicGenreVanity: json["music_genre_vanity"],
);
Map<String, dynamic> toJson() => {
"music_genre_id": musicGenreId,
"music_genre_parent_id": musicGenreParentId,
"music_genre_name": musicGenreName,
"music_genre_name_extended": musicGenreNameExtended,
"music_genre_vanity": musicGenreVanity,
};
}
class Header {
Header({
this.statusCode,
this.executeTime,
});
int statusCode;
double executeTime;
factory Header.fromJson(Map<String, dynamic> json) => Header(
statusCode: json["status_code"],
executeTime: json["execute_time"].toDouble(),
);
Map<String, dynamic> toJson() => {
"status_code": statusCode,
"execute_time": executeTime,
};
}
这是模型的用户界面:
import 'package:flutter/material.dart';
import 'package:json_parsing_example/models.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(home: HomePage());
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
List<TrackList> trackList = List();
bool _isLoading = false;
@override
void initState() {
super.initState();
getData();
}
getData() async {
setState(() {
_isLoading = true;
});
String data =
await DefaultAssetBundle.of(context).loadString("json/parse.json");
final track = trackFromJson(data);
trackList = track.message.body.trackList;
setState(() {
_isLoading = false;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: _isLoading
? Center(
child: CircularProgressIndicator(),
)
: ListView.builder(
itemCount: trackList.length,
itemBuilder: (context, index) {
var item = trackList[index];
return Card(
margin: EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Track Id :' + item.track.trackId.toString()),
Text('Track Name:' + item.track.trackName.toString()),
Text(
'Artist Name :' + item.track.artistName.toString()),
Text('Album Name :' + item.track.albumName.toString())
],
),
);
}));
}
}
从该功能中可以看到,您可以这样做:
Future<List<TrackList>> getData() async {
var response = await http.get(url1);
print("ssssssssssssssss");
print(response.body);
var item = trackFromJson(response.body);
return item.body.track_list;
}
查看示例,让我知道它是否有效。
答案 1 :(得分:0)
方法是创建自己的模型类,在其中执行解析机制。示例之一是创建如下的用户类:
{
"0": {
"created_at": "Sat May 09 23:57:13 +0000 2020",
"id_str": "1259271234912522240",
"text": "@VitalVegas Not sure if you\u2019ve seen this, but seems relevant\u2014,
"in_reply_to_status_id_str": "1259203897538654208",
"in_reply_to_user_id_str": "514487309",
"in_reply_to_screen_name": "VitalVegas",
"retweet_count": 0,
"favorite_count": 0,
"lang": "en",
"quoted_status_id_str": "1259178497320185856",
"hashtags": [],
"user_mentions": [
"514487309"
],
"user_id": "23156407",
"screen_name": "azzabazazz",
"followers_count": 321,
"friends_count": 846,
"favourites_count": 6292,
"statuses_count": 2895
},
"1": {
"created_at": "Sat May 09 23:57:14 +0000 2020",
"id_str": "1259271241430695937",
"text": "RT @GABI6here: #SaturdayThoughts WAKE-UP REMEMBER: Spread word on HouseBill to allow entering our house to test for #coronavirus >SO ban\ud83d\uded1 \ud83d\udc41\u2026",
"in_reply_to_status_id_str": null,
"in_reply_to_user_id_str": null,
"in_reply_to_screen_name": null,
"retweet_count": 30,
"favorite_count": 0,
"lang": "en",
"quoted_status_id_str": "1259242130062618624",
"hashtags": [
"SaturdayThoughts",
"coronavirus"
],
"user_mentions": [
"1229162888478150657"
],
"user_id": "756894930270904320",
"screen_name": "Lily4ever3",
"followers_count": 23862,
"friends_count": 23293,
"favourites_count": 12527,
"statuses_count": 36183
}
}
创建完可以容纳上面发布的整个树的基本模型之后,继续进行操作,并通过创建列出所有轨道属性的class User {
String firstName;
String lastName;
String phoneNumber;
String emailAddress;
User({
this.firstName,
this.lastName,
this.phoneNumber,
this.emailAddress,
});
factory User.fromJson(Map<String, dynamic> json) {
return User(
firstName: json["first_name"],
lastName: json["last_name"],
phoneNumber: json["phone_number"],
emailAddress: json["email_address"],
);
}
}
模型来解析诸如track_list
之类的数组元素。有了基本模型,跟踪模型和跟踪模型的所有属性后,您需要将该跟踪列表链接到您的基本模型,并按以下步骤完成:
Track
完成基本模型的链接,轨道列表和单个轨道的解析后,您还需要以相同的方式解析内部Track列表,但是我上面列出的解析机制的基础应该足以让您了解解析JSON对象和JSON内部列表对象的整个过程。