从mongodb反序列化几何多边形



我是flutter的新手,曾试图从我的api中反序列化一个geojson。

检索到的geojson看起来像这样:

[
{
"_id": {
"$oid": "5e95d60049ebb0e6b45a34e6"
},
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
14.810392700000136,
50.8584471640001
],
[
14.867856893000067,
50.8643899540001
]
]
]
},
"properties": {
"ADMIN": "Test",
"ISO_A3": "TST",
"ISO_A2": "TS"
}
}
]

我尝试使用JSON到Dart页面(https://javiercbk.github.io/json_to_dart/)检索一个我可以在flutter中使用的模型。。但它说生成的代码是无效的,因为坐标元素内有3个列表

生成的代码中的问题就在这里:

Geometry.fromJson(Map<String, dynamic> json) {
type = json['type'];
if (json['coordinates'] != null) {
coordinates = new List<List>();
json['coordinates'].forEach((v) { coordinates.add(new List.fromJson(v)); });
}
}

有人能帮我解决列表问题中的3个列表吗?

您可以在下面复制粘贴运行完整代码
您可以在完整代码中看到Geometry类定义
代码片段

class Geometry {
String type;
List<List<List<double>>> coordinates;
...  
factory Geometry.fromJson(Map<String, dynamic> json) => Geometry(
type: json["type"],
coordinates: List<List<List<double>>>.from(json["coordinates"].map(
(x) => List<List<double>>.from(
x.map((x) => List<double>.from(x.map((x) => x.toDouble())))))),
);

print(payloadList[0].id.oid);
print(payloadList[0].geometry.coordinates[0][0][0]);
print(payloadList[0].geometry.coordinates[0][0][1]);
print(payloadList[0].geometry.coordinates[0][1][0]);
print(payloadList[0].geometry.coordinates[0][1][1]);

输出

I/flutter (25078): 5e95d60049ebb0e6b45a34e6
I/flutter (25078): 14.810392700000136
I/flutter (25078): 50.8584471640001
I/flutter (25078): 14.867856893000067
I/flutter (25078): 50.8643899540001

全代码

import 'package:flutter/material.dart'; 
import 'dart:convert';
List<Payload> payloadFromJson(String str) =>
List<Payload>.from(json.decode(str).map((x) => Payload.fromJson(x)));
String payloadToJson(List<Payload> data) =>
json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class Payload {
Id id;
String type;
Geometry geometry;
Properties properties;
Payload({
this.id,
this.type,
this.geometry,
this.properties,
});
factory Payload.fromJson(Map<String, dynamic> json) => Payload(
id: Id.fromJson(json["_id"]),
type: json["type"],
geometry: Geometry.fromJson(json["geometry"]),
properties: Properties.fromJson(json["properties"]),
);
Map<String, dynamic> toJson() => {
"_id": id.toJson(),
"type": type,
"geometry": geometry.toJson(),
"properties": properties.toJson(),
};
}
class Geometry {
String type;
List<List<List<double>>> coordinates;
Geometry({
this.type,
this.coordinates,
});
factory Geometry.fromJson(Map<String, dynamic> json) => Geometry(
type: json["type"],
coordinates: List<List<List<double>>>.from(json["coordinates"].map(
(x) => List<List<double>>.from(
x.map((x) => List<double>.from(x.map((x) => x.toDouble())))))),
);
Map<String, dynamic> toJson() => {
"type": type,
"coordinates": List<dynamic>.from(coordinates.map((x) =>
List<dynamic>.from(
x.map((x) => List<dynamic>.from(x.map((x) => x)))))),
};
}
class Id {
String oid;
Id({
this.oid,
});
factory Id.fromJson(Map<String, dynamic> json) => Id(
oid: json["u0024oid"],
);
Map<String, dynamic> toJson() => {
"u0024oid": oid,
};
}
class Properties {
String admin;
String isoA3;
String isoA2;
Properties({
this.admin,
this.isoA3,
this.isoA2,
});
factory Properties.fromJson(Map<String, dynamic> json) => Properties(
admin: json["ADMIN"],
isoA3: json["ISO_A3"],
isoA2: json["ISO_A2"],
);
Map<String, dynamic> toJson() => {
"ADMIN": admin,
"ISO_A3": isoA3,
"ISO_A2": isoA2,
};
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
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;
void _incrementCounter() {
String jsonString = '''
[
{
"_id": {
"$oid": "5e95d60049ebb0e6b45a34e6"
},
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
14.810392700000136,
50.8584471640001
],
[
14.867856893000067,
50.8643899540001
]
]
]
},
"properties": {
"ADMIN": "Test",
"ISO_A3": "TST",
"ISO_A2": "TS"
}
}
]
''';
List<Payload> payloadList = payloadFromJson(jsonString);
print(payloadList[0].id.oid);
print(payloadList[0].geometry.coordinates[0][0][0]);
print(payloadList[0].geometry.coordinates[0][0][1]);
print(payloadList[0].geometry.coordinates[0][1][0]);
print(payloadList[0].geometry.coordinates[0][1][1]);
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),
),
);
}
}

最新更新