- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对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"
}
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));
},
),
],
),
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>
最佳答案
您可以在下面复制粘贴运行完整代码
你可以这样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/
我正在用来自JSON文件的一些数据填充Flutter中的列表。 但是,我的代码不断抛出异常"NoSuchMethodError (NoSuchMethodError: The method 'add'
通过eclipse运行Tomcat 7报错是: javax.servlet.ServletException: java.lang.NoSuchMethodError: org.eclipse.jdt
这是我的错误行: 这是我的代码: 最佳答案 final jobs= json.decode(response.body)['name_database_table']; 关于mobile - NoSu
很难说出这里问的是什么。这个问题是模棱两可的、模糊的、不完整的、过于宽泛的或修辞的,无法以目前的形式得到合理的回答。为了帮助澄清这个问题以便可以重新打开它,visit the help center
我已经被这个错误困扰了几个小时。。我的pom.xml。应用程序未启动。所有的Spring框架依赖于相同的版本,但仍然得到相同的错误。。更新。MVN依赖的结果:树。看起来这里一切都很好。
我得到: NoSuchMethodError: com.foo.SomeService.doSmth()Z 我是否正确理解这个'Z'意味着doSmth()方法的返回类型是 boolean 值?如果为
我在 Speed 类中引用 PlayerUtil.getMovementSpeed(player);,在我的 PlayerUtil 类中,我将方法定义为: public static double g
我得到: NoSuchMethodError: com.foo.SomeService.doSmth()Z 我是否正确理解这个 'Z' 意味着 doSmth() 方法的返回类型是 boolean 值?
我在使用 Spark 和 Scala 时遇到了一个奇怪的错误。我有一段代码声明了一个变量: var offset = 0 这会导致以下异常: java.lang.NoSuchMethodError:
我已经成功实现了 reflectionEquals 方法,其中包含一个排除字段列表。 return EqualsBuilder.reflectionEquals(this, obj, new Str
我正在使用 Spring 框架和 Maven 开发 Java Enterprise 应用程序。我正在为此学习一门类(class),并且一直坚持集成 Hibernate JPA。当我运行项目时,它返回以
I/flutter ( 8282): The following NoSuchMethodError was thrown building Meme(dirty, state: _MemeState
运行以下代码时出现 NoSuchMethodError - 我想从 JSON url 打印出轨道标题 - 我错过了什么吗? import 'dart:async'; import 'dart:conv
我正在做 Searchview flutter 中的例子 https://github.com/MageshPandian20/Flutter-SearchView 但我想对 进行更改子项类有一个 最
尝试从Eclipse中的简单Java程序连接到Hive时出现以下错误。看起来好像连接,然后引发此错误。我可以通过beeline在本地连接到Hive Thrift服务器,而不会出现问题。 两个libth
当我向安全资源发出请求时,会发生NoSuchMethodError。 基于基于Spring Boot 1.4.4的Grails 3.2.5的项目 AppConfig: @EnableWebSecuri
这个问题已经有答案了: Differences between Exception and Error (11 个回答) 已关闭 7 年前。 我的印象是 Exception 非常适合捕获所有可能的异常
祝大家有美好的一天!我使用 google Vision API,当我在 IntelliJ Idea 中运行我的程序时,它工作得很好,但是当我编译 jar 文件时,它在处理照片时给出错误 java.la
我一直在为这个问题苦苦挣扎。我正在开发一个包含很多包的 netbeans java 项目,起初我更改了 gui,但是当我运行代码时,它没有反射(reflect)任何更改,即使我在保存、清理、清理和编译
我一直在寻找问题的解决方案,但没有得到足够的答案。 我正在开发 Bukkit插件的更新系统。因此,我必须自己编写这些类的代码。但我一直想从 debug(String) 调用一个方法(具体来说: ano
我是一名优秀的程序员,十分优秀!