如何筛选 Json 数据并使用其他方法 |功能 | 类?如何将 Json 数据放入列表?
how to filter Json data and use other methods | function | classes? How get Json data into list?
Future<List<AdMob>>getData() async {
final String url =
"https://raw.githubusercontent.com/awais939869/AdsJson/main/speedifypro.json";
http.Response response = await http.get(Uri.parse(url));
// http.Response response = await http.get(Uri.parse("https://raw.githubusercontent.com/Hammad46/app/main/admob.json"));
var jsonObject = json.decode(response.body);
List <dynamic> data = (jsonObject as Map<String, dynamic>)['AdMob'];
List <AdMob> listData = [];
for (int i=0; i<data.length; i++)
listData.add(AdMob.fromJson(data[i]));
return listData;
}
ERROR
E/flutter ( 6315): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List'
JSON data
{
“AdMob”:{
“旗帜”:“真实”,
“插页式”:“真实”
}
}
AdMob
不是列表。这是一张地图。因此,如果您希望您的代码正常工作,您的 JSON 应该如下所示:
{
"AdMob": [
{
"Banner": "true",
"Interstitial": "true"
}
]
}
Future<List<AdMob>>getData() async {
final String url =
"https://raw.githubusercontent.com/awais939869/AdsJson/main/speedifypro.json";
http.Response response = await http.get(Uri.parse(url));
// http.Response response = await http.get(Uri.parse("https://raw.githubusercontent.com/Hammad46/app/main/admob.json"));
var jsonObject = json.decode(response.body);
List <dynamic> data = (jsonObject as Map<String, dynamic>)['AdMob'];
List <AdMob> listData = [];
for (int i=0; i<data.length; i++)
listData.add(AdMob.fromJson(data[i]));
return listData;
}
ERROR E/flutter ( 6315): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List'
JSON data {
“AdMob”:{ “旗帜”:“真实”, “插页式”:“真实” } }
AdMob
不是列表。这是一张地图。因此,如果您希望您的代码正常工作,您的 JSON 应该如下所示:
{
"AdMob": [
{
"Banner": "true",
"Interstitial": "true"
}
]
}