gpt4 book ai didi

json - 在Flutter/Dart中解析JSON:()=> Map ,NoSuchMethodError-(数组问题?)

转载 作者:行者123 更新时间:2023-12-03 03:00:36 29 4
gpt4 key购买 nike

我对Flutter / Dart还是有点陌生​​,而Ive在相当长的一段时间内一直在努力解析JSON。对我来说,这似乎是一项艰巨的任务,尽管我认为我的JSON结构并不那么复杂。

您的帮助将不胜感激。

这是我要解析的JSON:

{
"predictionICL":{
"openingTimeToday":"8:00 - 23:00",
"openingTimeTomorrow":"8:00 - 23:00",
"percentagesToday":[
3,
5,
11,
17,
20,
23,
25,
26,
25,
29,
30,
32,
31,
35,
40,
43,
44,
46,
49,
53,
50,
56,
54,
60,
62,
61,
69,
70,
75,
76,
84,
90,
94,
100,
93,
81,
72,
70,
73,
71,
63,
59,
55,
56,
51,
49,
50,
45,
43,
40,
38,
35,
31,
27,
25,
23,
20,
20,
19,
17,
12,
9,
8,
2,
1
],
"percentagesTomorrow":[
0,
0,
1,
7,
14,
20,
22,
21,
21,
22,
20,
25,
27,
31,
30,
31,
32,
33,
30,
34,
35,
33,
35,
37,
39,
40,
40,
39,
38,
40,
41,
38,
34,
37,
34,
35,
33,
32,
31,
30,
33,
30,
31,
30,
29,
30,
27,
28,
26,
23,
20,
19,
16,
17,
15,
12,
10,
7,
5,
1,
1,
0,
0,
0,
0
]
},
"predictionRandwyck":{
"openingTimeToday":"8:00 - 23:00",
"openingTimeTomorrow":"8:00 - 23:00",
"percentagesToday":[
3,
5,
11,
17,
20,
23,
25,
26,
25,
29,
30,
32,
31,
35,
40,
43,
44,
46,
49,
53,
50,
56,
54,
60,
62,
61,
69,
70,
75,
76,
84,
90,
94,
100,
93,
81,
72,
70,
73,
71,
63,
59,
55,
56,
51,
49,
50,
45,
43,
40,
38,
35,
31,
27,
25,
23,
20,
20,
19,
17,
12,
9,
8,
2,
1
],
"percentagesTomorrow":[
0,
0,
1,
7,
14,
20,
22,
21,
21,
22,
20,
25,
27,
31,
30,
31,
32,
33,
30,
34,
35,
33,
35,
37,
39,
40,
40,
39,
38,
40,
41,
38,
34,
37,
34,
35,
33,
32,
31,
30,
33,
30,
31,
30,
29,
30,
27,
28,
26,
23,
20,
19,
16,
17,
15,
12,
10,
7,
5,
1,
1,
0,
0,
0,
0
]
},
"predictionTapijn":{
"openingTimeToday":"8:00 - 23:00",
"openingTimeTomorrow":"8:00 - 23:00",
"percentagesToday":[
3,
5,
11,
17,
20,
23,
25,
26,
25,
29,
30,
32,
31,
35,
40,
43,
44,
46,
49,
53,
50,
56,
54,
60,
62,
61,
69,
70,
75,
76,
84,
90,
94,
100,
93,
81,
72,
70,
73,
71,
63,
59,
55,
56,
51,
49,
50,
45,
43,
40,
38,
35,
31,
27,
25,
23,
20,
20,
19,
17,
12,
9,
8,
2,
1
],
"percentagesTomorrow":[
0,
0,
1,
7,
14,
20,
22,
21,
21,
22,
20,
25,
27,
31,
30,
31,
32,
33,
30,
34,
35,
33,
35,
37,
39,
40,
40,
39,
38,
40,
41,
38,
34,
37,
34,
35,
33,
32,
31,
30,
33,
30,
31,
30,
29,
30,
27,
28,
26,
23,
20,
19,
16,
17,
15,
12,
10,
7,
5,
1,
1,
0,
0,
0,
0
]
},
"message":"optionalmessageString"
}

它基本上只是数据类型LibraryPrediction的三个实例和一个可选的Message字符串。

数据类型LibraryPrediction由一个字符串“openingTimeToday”,一个字符串“openingTimeTomorrow”和两个 double 数组“percentagesToday”和“percentagesTomorrow”组成。

现在,由于服务器尚未运行,我正在尝试从磁盘上方解析json。
到目前为止,这是我的代码:

我有一个服务文件:
import 'dart:convert';
import 'package:flutter/services.dart';
import 'package:test_app/models/predictions_update_model.dart';


PredictionsUpdate parseUpdate(String responseBody) {
final jsonResponse = json.decode(responseBody).cast<Map<String, dynamic>>();

PredictionsUpdate update = jsonResponse.map<PredictionsUpdate>((json) => PredictionsUpdate.fromJson(json));

return update;

}

Future<PredictionsUpdate> fetchUpdate() async {

final response = await rootBundle.loadString('lib/testJson/data.json');

return parseUpdate(response);
}

还有一个模型文件:
class PredictionsUpdate {
final LibraryPrediction predictionICL;
final LibraryPrediction predictionRandwyck;
final LibraryPrediction predictionTapijn;
final String message;

PredictionsUpdate({
this.predictionICL,
this.predictionRandwyck,
this.predictionTapijn,
this.message,
});

factory PredictionsUpdate.fromJson(Map<String, dynamic> parsedJson){
return PredictionsUpdate(
predictionICL: LibraryPrediction.fromJson(parsedJson['predictionICL']),
predictionRandwyck: LibraryPrediction.fromJson(parsedJson['predictionRandwyck']),
predictionTapijn: LibraryPrediction.fromJson(parsedJson['predictionTapijn']),
message: parsedJson['message'] as String,
);
}
}

class LibraryPrediction {
final String openingTimeToday;
final String openingTimeTomorrow;
final List<double> percentagesToday;
final List<double> percentagesTomorrow;

LibraryPrediction({
this.openingTimeToday,
this.openingTimeTomorrow,
this.percentagesToday,
this.percentagesTomorrow,
});

factory LibraryPrediction.fromJson(Map<String, dynamic> json){
return LibraryPrediction(
openingTimeToday: json['openingTimeToday'] as String,
openingTimeTomorrow: json['openingTimeTomorrow'] as String,
percentagesToday: json['percentagesToday'] as List<double>,
percentagesTomorrow: json['percentagesTomorrow'] as List<double>,
);
}
}

这就是我所谓的函​​数:

Row(
children: <Widget>[
RaisedButton(
child: Text('update'),
onPressed: () {
Future<PredictionsUpdate> futureUpdate = fetchUpdate();
futureUpdate.then((update)=> widget.currentNumbers = update)
.catchError((error) => print(error));
},
),
],
),


每当我尝试解析JSON时,都会出现以下错误:
flutter: NoSuchMethodError: Class '_InternalLinkedHashMap<String, dynamic>' has no instance method 'cast' with matching arguments.
Receiver: _LinkedHashMap len:4
Tried calling: cast<Map<String, dynamic>>()
Found: cast<RK, RV>() => Map<RK, RV>

我有种感觉,当我尝试解析“percentagesToday”或“percentagesTomorrow” double 数组时,该错误起源于某个地方,但是我不能肯定地说,我无法从错误消息中获取更多线索。

我非常感谢您找出我出了问题的地方。

最佳答案

您可以在下面复制粘贴运行完整代码
你可以这样List<double>.from(json["percentagesToday"].map((x) => x.toDouble())),
程式码片段

factory LibraryPrediction.fromJson(Map<String, dynamic> json) =>
LibraryPrediction(
openingTimeToday: json["openingTimeToday"],
openingTimeTomorrow: json["openingTimeTomorrow"],
percentagesToday: List<double>.from(
json["percentagesToday"].map((x) => x.toDouble())),
percentagesTomorrow: List<double>.from(
json["percentagesTomorrow"].map((x) => x.toDouble())),
);
...
futureUpdate.then((update) {
print('${update.predictionIcl.openingTimeToday.toString()}');
print('${update.message}');
print('${update.predictionRandwyck.openingTimeTomorrow}');
}).catchError((error) => print(error));

输出
I/flutter ( 7344): 8:00 - 23:00
I/flutter ( 7344): optionalmessageString
I/flutter ( 7344): 8:00 - 23:00

完整的代码
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';

PredictionsUpdate predictionsUpdateFromJson(String str) =>
PredictionsUpdate.fromJson(json.decode(str));

class PredictionsUpdate {
LibraryPrediction predictionIcl;
LibraryPrediction predictionRandwyck;
LibraryPrediction predictionTapijn;
String message;

PredictionsUpdate({
this.predictionIcl,
this.predictionRandwyck,
this.predictionTapijn,
this.message,
});

factory PredictionsUpdate.fromJson(Map<String, dynamic> json) =>
PredictionsUpdate(
predictionIcl: LibraryPrediction.fromJson(json["predictionICL"]),
predictionRandwyck:
LibraryPrediction.fromJson(json["predictionRandwyck"]),
predictionTapijn: LibraryPrediction.fromJson(json["predictionTapijn"]),
message: json["message"],
);
}

class LibraryPrediction {
String openingTimeToday;
String openingTimeTomorrow;
List<double> percentagesToday;
List<double> percentagesTomorrow;

LibraryPrediction({
this.openingTimeToday,
this.openingTimeTomorrow,
this.percentagesToday,
this.percentagesTomorrow,
});

factory LibraryPrediction.fromJson(Map<String, dynamic> json) =>
LibraryPrediction(
openingTimeToday: json["openingTimeToday"],
openingTimeTomorrow: json["openingTimeTomorrow"],
percentagesToday: List<double>.from(
json["percentagesToday"].map((x) => x.toDouble())),
percentagesTomorrow: List<double>.from(
json["percentagesTomorrow"].map((x) => x.toDouble())),
);
}

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;

Future<PredictionsUpdate> fetchUpdate() async {
String jsonString = '''
{
"predictionICL":{
"openingTimeToday":"8:00 - 23:00",
"openingTimeTomorrow":"8:00 - 23:00",
"percentagesToday":[
3,
5,
11,
17,
20,
23,
25,
26,
25,
29,
30,
32,
31,
35,
40,
43,
44,
46,
49,
53,
50,
56,
54,
60,
62,
61,
69,
70,
75,
76,
84,
90,
94,
100,
93,
81,
72,
70,
73,
71,
63,
59,
55,
56,
51,
49,
50,
45,
43,
40,
38,
35,
31,
27,
25,
23,
20,
20,
19,
17,
12,
9,
8,
2,
1
],
"percentagesTomorrow":[
0,
0,
1,
7,
14,
20,
22,
21,
21,
22,
20,
25,
27,
31,
30,
31,
32,
33,
30,
34,
35,
33,
35,
37,
39,
40,
40,
39,
38,
40,
41,
38,
34,
37,
34,
35,
33,
32,
31,
30,
33,
30,
31,
30,
29,
30,
27,
28,
26,
23,
20,
19,
16,
17,
15,
12,
10,
7,
5,
1,
1,
0,
0,
0,
0
]
},
"predictionRandwyck":{
"openingTimeToday":"8:00 - 23:00",
"openingTimeTomorrow":"8:00 - 23:00",
"percentagesToday":[
3,
5,
11,
17,
20,
23,
25,
26,
25,
29,
30,
32,
31,
35,
40,
43,
44,
46,
49,
53,
50,
56,
54,
60,
62,
61,
69,
70,
75,
76,
84,
90,
94,
100,
93,
81,
72,
70,
73,
71,
63,
59,
55,
56,
51,
49,
50,
45,
43,
40,
38,
35,
31,
27,
25,
23,
20,
20,
19,
17,
12,
9,
8,
2,
1
],
"percentagesTomorrow":[
0,
0,
1,
7,
14,
20,
22,
21,
21,
22,
20,
25,
27,
31,
30,
31,
32,
33,
30,
34,
35,
33,
35,
37,
39,
40,
40,
39,
38,
40,
41,
38,
34,
37,
34,
35,
33,
32,
31,
30,
33,
30,
31,
30,
29,
30,
27,
28,
26,
23,
20,
19,
16,
17,
15,
12,
10,
7,
5,
1,
1,
0,
0,
0,
0
]
},
"predictionTapijn":{
"openingTimeToday":"8:00 - 23:00",
"openingTimeTomorrow":"8:00 - 23:00",
"percentagesToday":[
3,
5,
11,
17,
20,
23,
25,
26,
25,
29,
30,
32,
31,
35,
40,
43,
44,
46,
49,
53,
50,
56,
54,
60,
62,
61,
69,
70,
75,
76,
84,
90,
94,
100,
93,
81,
72,
70,
73,
71,
63,
59,
55,
56,
51,
49,
50,
45,
43,
40,
38,
35,
31,
27,
25,
23,
20,
20,
19,
17,
12,
9,
8,
2,
1
],
"percentagesTomorrow":[
0,
0,
1,
7,
14,
20,
22,
21,
21,
22,
20,
25,
27,
31,
30,
31,
32,
33,
30,
34,
35,
33,
35,
37,
39,
40,
40,
39,
38,
40,
41,
38,
34,
37,
34,
35,
33,
32,
31,
30,
33,
30,
31,
30,
29,
30,
27,
28,
26,
23,
20,
19,
16,
17,
15,
12,
10,
7,
5,
1,
1,
0,
0,
0,
0
]
},
"message":"optionalmessageString"
}
''';

//final response = await rootBundle.loadString('lib/testJson/data.json');
final http.Response response = http.Response(jsonString, 200);
PredictionsUpdate payload = predictionsUpdateFromJson(response.body);
return payload;
}

void _incrementCounter() {
Future<PredictionsUpdate> futureUpdate = fetchUpdate();
futureUpdate.then((update) {
print('${update.predictionIcl.openingTimeToday.toString()}');
print('${update.message}');
print('${update.predictionRandwyck.openingTimeTomorrow}');
}).catchError((error) => print(error));

setState(() {
_counter++;
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}

关于json - 在Flutter/Dart中解析JSON:<RK,RV>()=> Map <RK,RV>,NoSuchMethodError-(数组问题?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61044155/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com