Json解码失败?
Json Decoding Failed?
https://picsum.photos/v2/list?page=1&limit=50
我正在使用这个 Json 文件,但是当我尝试解码时它显示为空。
我试过了
'''
List imgData;
final String url = 'https://picsum.photos/v2/list?page=1&limit=50';
Future getData() async {
var response = await http
.get(Uri.encodeFull(url), headers: {'Accept': 'application/json'});
List data = jsonDecode(response.body)['results'];
setState(() {
imgData = data;
});
}
@override
void initState() {
super.initState();
this.getData();
}
'''
尝试替换下面
List data = jsonDecode(response.body)['results'];
和
List data = jsonDecode(response.body);
您的 JSON
中没有 results 参数
您可以使用 JSON to dart tool,可免费在线使用。
将您的 JSON 粘贴到左侧面板并从右上角 select 飞镖语言,
您将获得您的 dart class 代码,您可以在其中使用 .toMap() 和 .toJson() 等方法,
而且速度非常快,人为错误较少。
这对于巨大的 JSON 数据非常有帮助。
您将通过此工具获得 class 这样的数据。
// To parse this JSON data, do
//
// final data = dataFromJson(jsonString);
import 'dart:convert';
List<Data> dataFromJson(String str) => List<Data>.from(json.decode(str).map((x) => Data.fromMap(x)));
String dataToJson(List<Data> data) => json.encode(List<dynamic>.from(data.map((x) => x.toMap())));
class Data {
String id;
String author;
int width;
int height;
String url;
String downloadUrl;
Data({
this.id,
this.author,
this.width,
this.height,
this.url,
this.downloadUrl,
});
factory Data.fromMap(Map<String, dynamic> json) => Data(
id: json["id"],
author: json["author"],
width: json["width"],
height: json["height"],
url: json["url"],
downloadUrl: json["download_url"],
);
Map<String, dynamic> toMap() => {
"id": id,
"author": author,
"width": width,
"height": height,
"url": url,
"download_url": downloadUrl,
};
}
之后就可以使用dataFromJson方法了。
这可以是您代码的完整实现。
import 'dart:convert';
List imgData;
final String url = 'https://picsum.photos/v2/list?page=1&limit=50';
Future getData() async {
var response = await http
.get(Uri.encodeFull(url), headers: {'Accept': 'application/json'});
List data = dataFromJson(response.body);
setState(() {
imgData = data;
});
}
@override
void initState() {
super.initState();
this.getData();
}
List<Data> dataFromJson(String str) => List<Data>.from(json.decode(str).map((x) => Data.fromMap(x)));
String dataToJson(List<Data> data) => json.encode(List<dynamic>.from(data.map((x) => x.toMap())));
class Data {
String id;
String author;
int width;
int height;
String url;
String downloadUrl;
Data({
this.id,
this.author,
this.width,
this.height,
this.url,
this.downloadUrl,
});
factory Data.fromMap(Map<String, dynamic> json) => Data(
id: json["id"],
author: json["author"],
width: json["width"],
height: json["height"],
url: json["url"],
downloadUrl: json["download_url"],
);
Map<String, dynamic> toMap() => {
"id": id,
"author": author,
"width": width,
"height": height,
"url": url,
"download_url": downloadUrl,
};
}
https://picsum.photos/v2/list?page=1&limit=50 我正在使用这个 Json 文件,但是当我尝试解码时它显示为空。
我试过了
'''
List imgData;
final String url = 'https://picsum.photos/v2/list?page=1&limit=50';
Future getData() async {
var response = await http
.get(Uri.encodeFull(url), headers: {'Accept': 'application/json'});
List data = jsonDecode(response.body)['results'];
setState(() {
imgData = data;
});
}
@override
void initState() {
super.initState();
this.getData();
}
'''
尝试替换下面
List data = jsonDecode(response.body)['results'];
和
List data = jsonDecode(response.body);
您的 JSON
中没有 results 参数您可以使用 JSON to dart tool,可免费在线使用。
将您的 JSON 粘贴到左侧面板并从右上角 select 飞镖语言,
您将获得您的 dart class 代码,您可以在其中使用 .toMap() 和 .toJson() 等方法,
而且速度非常快,人为错误较少。
这对于巨大的 JSON 数据非常有帮助。
您将通过此工具获得 class 这样的数据。
// To parse this JSON data, do
//
// final data = dataFromJson(jsonString);
import 'dart:convert';
List<Data> dataFromJson(String str) => List<Data>.from(json.decode(str).map((x) => Data.fromMap(x)));
String dataToJson(List<Data> data) => json.encode(List<dynamic>.from(data.map((x) => x.toMap())));
class Data {
String id;
String author;
int width;
int height;
String url;
String downloadUrl;
Data({
this.id,
this.author,
this.width,
this.height,
this.url,
this.downloadUrl,
});
factory Data.fromMap(Map<String, dynamic> json) => Data(
id: json["id"],
author: json["author"],
width: json["width"],
height: json["height"],
url: json["url"],
downloadUrl: json["download_url"],
);
Map<String, dynamic> toMap() => {
"id": id,
"author": author,
"width": width,
"height": height,
"url": url,
"download_url": downloadUrl,
};
}
之后就可以使用dataFromJson方法了。
这可以是您代码的完整实现。
import 'dart:convert';
List imgData;
final String url = 'https://picsum.photos/v2/list?page=1&limit=50';
Future getData() async {
var response = await http
.get(Uri.encodeFull(url), headers: {'Accept': 'application/json'});
List data = dataFromJson(response.body);
setState(() {
imgData = data;
});
}
@override
void initState() {
super.initState();
this.getData();
}
List<Data> dataFromJson(String str) => List<Data>.from(json.decode(str).map((x) => Data.fromMap(x)));
String dataToJson(List<Data> data) => json.encode(List<dynamic>.from(data.map((x) => x.toMap())));
class Data {
String id;
String author;
int width;
int height;
String url;
String downloadUrl;
Data({
this.id,
this.author,
this.width,
this.height,
this.url,
this.downloadUrl,
});
factory Data.fromMap(Map<String, dynamic> json) => Data(
id: json["id"],
author: json["author"],
width: json["width"],
height: json["height"],
url: json["url"],
downloadUrl: json["download_url"],
);
Map<String, dynamic> toMap() => {
"id": id,
"author": author,
"width": width,
"height": height,
"url": url,
"download_url": downloadUrl,
};
}