Flutter 我怎样才能深入 json 文件
Flutter how can i go deeper in the json file
我想更深入地了解 JSON 文件我需要如何更改代码。
因为我需要更深入地了解数据 -> 资源 -> 项目 -> 标题
My JSON:
{
"status":"UPDATE",
"data":{
"version":"2",
"modDate":"2021-12-22T17:33:59+0100",
"languages":[
"DE",
"EN"
],
"mapmarker":[
],
"resources":[
{
"id":241,
"type":"image",
"items":[
{
"lang":"DE",
"title":"Labor",
"file":"uploads\/",
"size":1923217,
"modDate":"2019-02-28T18:07:46+0100"
},
{
"lang":"EN",
"title":"Faust Labor",
"file":"uploads\/",
"size":1923217,
"modDate":"2019-02-28T18:07:46+0100"
}
]
},
我的不工作实际代码:
Future<void> getDaten() async {
final response =
await http.get(Uri.parse("https://blabla"));
final extractedData = json.decode(response.body) as Map<String, dynamic>;
var deviceData = extractedData["data"] as Map<String, dynamic>;
var deviceData2 = deviceData["resources"] as Map<String, dynamic>;
deviceData["items"].forEach((e)=>print(e["text"]));
}
我现有的错误:
Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' in type cast
我只能转到数据 -> 资源 -> 项目,然后我只能得到这样的字符串
[{lang: DE, title: Schmuck, Uhren und Spiele, file: uploads/Spiele.JPG, size: 774265, modDate: 2021-12-13T16:04:25+0100}]
如果您想提取资源中所有项目的所有 titles
,您可以这样做。请注意,我们不需要强制转换我们使用的每个值,因为 dynamic
意味着 Dart 应该忽略我们对对象所做的事情,并期望对 dynamic
的任何调用也是 dynamic
:
import 'dart:convert';
void main() {
dynamic jsonObject = jsonDecode(jsonString);
final titles = [
for (Map<String, dynamic> resource in jsonObject['data']['resources'])
for (Map<String, dynamic> item in resource['items'])
item['title'] as String
];
titles.forEach(print);
// Labor
// Faust Labor
}
const jsonString = '''{
"status": "UPDATE",
"data": {
"version": "2",
"modDate": "2021-12-22T17:33:59+0100",
"languages": [
"DE",
"EN"
],
"mapmarker": [
],
"resources": [
{
"id": 241,
"type": "image",
"items": [
{
"lang": "DE",
"title": "Labor",
"file": "uploads\/",
"size": 1923217,
"modDate": "2019-02-28T18:07:46+0100"
},
{
"lang": "EN",
"title": "Faust Labor",
"file": "uploads\/",
"size": 1923217,
"modDate": "2019-02-28T18:07:46+0100"
}
]
}
]
}
}
''';
在你的函数下面的行中
var deviceData = extractedData["data"] as Map<String, dynamic>;
var deviceData2 = deviceData["resources"] as Map<String, dynamic>;
导致错误,因为在你的 API extractedData["data"]
中 returning
你是 List
但你将其转换为 Map<String, dynamic>
这就是它抛出的原因错误。要像这样解决这个问题:
var deviceData = extractedData["data"];
var deviceData2 = deviceData["resources"];
我想更深入地了解 JSON 文件我需要如何更改代码。
因为我需要更深入地了解数据 -> 资源 -> 项目 -> 标题
My JSON:
{
"status":"UPDATE",
"data":{
"version":"2",
"modDate":"2021-12-22T17:33:59+0100",
"languages":[
"DE",
"EN"
],
"mapmarker":[
],
"resources":[
{
"id":241,
"type":"image",
"items":[
{
"lang":"DE",
"title":"Labor",
"file":"uploads\/",
"size":1923217,
"modDate":"2019-02-28T18:07:46+0100"
},
{
"lang":"EN",
"title":"Faust Labor",
"file":"uploads\/",
"size":1923217,
"modDate":"2019-02-28T18:07:46+0100"
}
]
},
我的不工作实际代码:
Future<void> getDaten() async {
final response =
await http.get(Uri.parse("https://blabla"));
final extractedData = json.decode(response.body) as Map<String, dynamic>;
var deviceData = extractedData["data"] as Map<String, dynamic>;
var deviceData2 = deviceData["resources"] as Map<String, dynamic>;
deviceData["items"].forEach((e)=>print(e["text"]));
}
我现有的错误:
Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' in type cast
我只能转到数据 -> 资源 -> 项目,然后我只能得到这样的字符串
[{lang: DE, title: Schmuck, Uhren und Spiele, file: uploads/Spiele.JPG, size: 774265, modDate: 2021-12-13T16:04:25+0100}]
如果您想提取资源中所有项目的所有 titles
,您可以这样做。请注意,我们不需要强制转换我们使用的每个值,因为 dynamic
意味着 Dart 应该忽略我们对对象所做的事情,并期望对 dynamic
的任何调用也是 dynamic
:
import 'dart:convert';
void main() {
dynamic jsonObject = jsonDecode(jsonString);
final titles = [
for (Map<String, dynamic> resource in jsonObject['data']['resources'])
for (Map<String, dynamic> item in resource['items'])
item['title'] as String
];
titles.forEach(print);
// Labor
// Faust Labor
}
const jsonString = '''{
"status": "UPDATE",
"data": {
"version": "2",
"modDate": "2021-12-22T17:33:59+0100",
"languages": [
"DE",
"EN"
],
"mapmarker": [
],
"resources": [
{
"id": 241,
"type": "image",
"items": [
{
"lang": "DE",
"title": "Labor",
"file": "uploads\/",
"size": 1923217,
"modDate": "2019-02-28T18:07:46+0100"
},
{
"lang": "EN",
"title": "Faust Labor",
"file": "uploads\/",
"size": 1923217,
"modDate": "2019-02-28T18:07:46+0100"
}
]
}
]
}
}
''';
在你的函数下面的行中
var deviceData = extractedData["data"] as Map<String, dynamic>;
var deviceData2 = deviceData["resources"] as Map<String, dynamic>;
导致错误,因为在你的 API extractedData["data"]
中 returning
你是 List
但你将其转换为 Map<String, dynamic>
这就是它抛出的原因错误。要像这样解决这个问题:
var deviceData = extractedData["data"];
var deviceData2 = deviceData["resources"];