我正在尝试写一个函数来解析json文件,并向我展示相关的infor,比如Sid
,Star
和Approach
,现在这是json文件,我的主文件有按钮,将加载一个列表视图,我选择的选项,
{
"@ICAOcode": "VNJP",
"Sid": [
{
"@Name": "DAMA1A",
"@Runways": "27",
"Sid_Waypoint": [
{
"@ID": "1",
"Name": "(2000)",
"Type": "ConstHdgtoAlt",
"Latitude": "0.000000",
"Longitude": "0.000000",
"Speed": "0",
"Altitude": "2000",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Hdg_Crs": "0",
"Hdg_Crs_value": "271",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
},
{
"@ID": "2",
"Name": "LALBA",
"Type": "Normal",
"Latitude": "26.947222",
"Longitude": "85.806389",
"Speed": "0",
"Altitude": "7500",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Right"
},
{
"@ID": "3",
"Name": "DAMAR",
"Type": "Normal",
"Latitude": "27.255850",
"Longitude": "85.662892",
"Speed": "0",
"Altitude": "12500",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
}
]
},
{
"@Name": "DAMA1B",
"@Runways": "09",
"Sid_Waypoint": [
{
"@ID": "1",
"Name": "(2000)",
"Type": "ConstHdgtoAlt",
"Latitude": "0.000000",
"Longitude": "0.000000",
"Speed": "0",
"Altitude": "2000",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Hdg_Crs": "0",
"Hdg_Crs_value": "091",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
},
{
"@ID": "2",
"Name": "LALBA",
"Type": "Normal",
"Latitude": "26.947222",
"Longitude": "85.806389",
"Speed": "0",
"Altitude": "7500",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Left"
},
{
"@ID": "3",
"Name": "DAMAR",
"Type": "Normal",
"Latitude": "27.255850",
"Longitude": "85.662892",
"Speed": "0",
"Altitude": "12500",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
}
]
},
{
"@Name": "THIL1A",
"@Runways": "09",
"Sid_Waypoint": [
{
"@ID": "1",
"Name": "(1000)",
"Type": "ConstHdgtoAlt",
"Latitude": "0.000000",
"Longitude": "0.000000",
"Speed": "0",
"Altitude": "1000",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Hdg_Crs": "0",
"Hdg_Crs_value": "091",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
},
{
"@ID": "2",
"Name": "THILA",
"Type": "Normal",
"Latitude": "26.790619",
"Longitude": "86.095494",
"Speed": "0",
"Altitude": "3000",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Left"
}
]
}
],
"Star": [
{
"@Name": "DAMA1C",
"@Runways": "09",
"Star_Waypoint": [
{
"@ID": "1",
"Name": "DAMAR",
"Type": "Normal",
"Latitude": "27.255850",
"Longitude": "85.662892",
"Speed": "0",
"Altitude": "13500",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
},
{
"@ID": "2",
"Name": "LALBA",
"Type": "Normal",
"Latitude": "26.947222",
"Longitude": "85.806389",
"Speed": "0",
"Altitude": "7500",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
},
{
"@ID": "3",
"Name": "MEGHA",
"Type": "Normal",
"Latitude": "26.882486",
"Longitude": "85.836403",
"Speed": "0",
"Altitude": "6000",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
}
]
},
{
"@Name": "DAMA1D",
"@Runways": "27",
"Star_Waypoint": [
{
"@ID": "1",
"Name": "DAMAR",
"Type": "Normal",
"Latitude": "27.255850",
"Longitude": "85.662892",
"Speed": "0",
"Altitude": "13500",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
},
{
"@ID": "2",
"Name": "LALBA",
"Type": "Normal",
"Latitude": "26.947222",
"Longitude": "85.806389",
"Speed": "180",
"Altitude": "7500",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
},
{
"@ID": "3",
"Name": "THILA",
"Type": "Normal",
"Latitude": "26.790619",
"Longitude": "86.095494",
"Speed": "180",
"Altitude": "3000",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
},
{
"@ID": "4",
"Name": "DHABA",
"Type": "Normal",
"Latitude": "26.707047",
"Longitude": "86.095917",
"Speed": "0",
"Altitude": "3000",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
}
]
}
],
"Approach": {
"@Name": "RNV09",
"App_Waypoint": [
{
"@ID": "1",
"Name": "JP962",
"Type": "Normal",
"Latitude": "26.710039",
"Longitude": "85.785028",
"Speed": "0",
"Altitude": "2200",
"AltitudeCons": "0",
"AltitudeRestriction": "at",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
},
{
"@ID": "2",
"Name": "JP961",
"Type": "Normal",
"Latitude": "26.709417",
"Longitude": "85.855842",
"Speed": "0",
"Altitude": "1300",
"AltitudeCons": "0",
"AltitudeRestriction": "at",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
},
{
"@ID": "3",
"Name": "RW09",
"Type": "Runway",
"Latitude": "26.708869",
"Longitude": "85.915303",
"Speed": "0",
"Altitude": "286",
"AltitudeCons": "0",
"AltitudeRestriction": "at",
"Flytype": "Fly-over",
"BankLimit": "25",
"Sp_Turn": "Auto"
},
{
"@ID": "4",
"Name": "DHABA",
"Type": "Normal",
"Latitude": "26.707047",
"Longitude": "86.095917",
"Speed": "0",
"Altitude": "3000",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
},
{
"@ID": "5",
"Name": "DHABA",
"Type": "Hold",
"Latitude": "26.707047",
"Longitude": "86.095917",
"Speed": "160",
"Altitude": "3000",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Flytype": "Fly-by",
"BankLimit": "25",
"Hld_Rad_or_Inbd": "Inbd",
"Hld_Rad_value": "266",
"Hld_Turn": "Right",
"Hld_Time_or_Dist": "Time",
"Hld_td_value": "1.0",
"Sp_Turn": "Right"
}
],
"App_Transition": {
"@Name": "MEGHA",
"AppTr_Waypoint": [
{
"@ID": "1",
"Name": "MEGHA",
"Type": "Normal",
"Latitude": "26.882486",
"Longitude": "85.836403",
"Speed": "0",
"Altitude": "6000",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
},
{
"@ID": "2",
"Name": "JP963",
"Type": "Normal",
"Latitude": "26.793614",
"Longitude": "85.784606",
"Speed": "0",
"Altitude": "3800",
"AltitudeCons": "0",
"AltitudeRestriction": "at",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
}
]
}
}
}
这是到目前为止我写的代码来解析它,但我在尝试解析Approach
时出错。错误各不相同,最新的错误是[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'
,在下面的代码中的这一行for (final app in decodedData['Approach'].values) {
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:flutter/services.dart' show rootBundle;
class ProcedureProvider extends ChangeNotifier {
Future<List<ProcRoute>> extractAirportData(String filename) async {
final String jsonData = await rootBundle.loadString(filename);
final decodedData = json.decode(jsonData);
final List<ProcRoute> routes = [];
for (final sid in decodedData['Sid']) {
final List<Waypoint> waypoints = [];
for (final wp in sid['Sid_Waypoint']) {
waypoints.add(Waypoint(
wp['Name'],
wp['Type'],
double.parse(wp['Latitude']),
double.parse(wp['Longitude']),
double.parse(wp['Altitude']),
wp['AltitudeRestriction'],
wp['Flytype'],
));
}
routes.add(ProcRoute(sid['@Name'], sid['@Runways'], waypoints));
}
for (final star in decodedData['Star']) {
final List<Waypoint> waypoints = [];
for (final wp in star['Star_Waypoint']) {
waypoints.add(Waypoint(
wp['Name'],
wp['Type'],
double.parse(wp['Latitude']),
double.parse(wp['Longitude']),
double.parse(wp['Altitude']),
wp['AltitudeRestriction'],
wp['Flytype'],
));
}
routes.add(ProcRoute(star['@Name'], star['@Runways'], waypoints));
}
for (final app in decodedData['Approach'].values) {
final List<Waypoint> waypoints = [];
for (final wp in app['App_Waypoint']) {
waypoints.add(Waypoint(
wp['Name'],
wp['Type'],
double.parse(wp['Latitude']),
double.parse(wp['Longitude']),
double.parse(wp['Altitude']),
wp['AltitudeRestriction'],
wp['Flytype'],
));
}
routes.add(ProcRoute(app['@Name'], app['@Runways'], waypoints));
}
for (final appTrans in decodedData['App_Transition']) {
final List<Waypoint> waypoints = [];
for (final wp in appTrans['App_Transition_Waypoint']) {
waypoints.add(Waypoint(
wp['Name'],
wp['Type'],
double.parse(wp['Latitude']),
double.parse(wp['Longitude']),
double.parse(wp['Altitude']),
wp['AltitudeRestriction'],
wp['Flytype'],
));
}
routes.add(ProcRoute(appTrans['@Name'], appTrans['@Runways'], waypoints));
}
return routes;
}
List<String> getSids(List<ProcRoute> routes) {
final sids = <String>[];
for (final route in routes) {
if (route.name.startsWith('SID')) {
sids.add(route.name);
}
}
return sids;
}
}
class Waypoint {
final String name;
final String type;
final double latitude;
final double longitude;
final double altitude;
final String altitudeRestriction;
final String flyType;
Waypoint(this.name, this.type, this.latitude, this.longitude, this.altitude,
this.altitudeRestriction, this.flyType);
}
class ProcRoute {
final String name;
final String runway;
final List<Waypoint> waypoints;
ProcRoute(this.name, this.runway, this.waypoints);
}
我该怎么解决?
2条答案
按热度按时间b4lqfgs41#
你可以尝试使用这个类来解码你的数据:
这样使用它:
您可以使用this online tool创建类似于上面使用json数据的Model类。
vuv7lop32#
为什么Sid,星星是JSON文件中的数组,而Approach不是?https://www.edureka.co/community/83636/how-to-use-for-loop-in-flutter For in loop“它主要用于迭代数组或集合的元素。“
你应该在你的Waypoint类中写一个工厂函数'fromJson',让你的代码更干净更小。