gpt4 book ai didi

json - Flutter与Distance Matrix API解析json

转载 作者:IT王子 更新时间:2023-10-29 06:54:23 27 4
gpt4 key购买 nike

我目前正在构建一个 flutter 应用程序,我想在其中计算某些对象之间的距离,并使用 Google Distance Matrix API 来执行此操作。我在使用 Dart 解析 json 时遇到问题。我最终想要的只是一个与 json 结果的距离列表,以便我可以对它们进行索引并将它们应用于我的应用程序中的数据。

json 结果如下所示:

{
"destination_addresses" : [
"destination address",
"destination address"
],
"origin_addresses" : [ "Origin addresses here" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "4.3 mi",
"value" : 6998
},
"duration" : {
"text" : "14 mins",
"value" : 848
},
"status" : "OK"
},
{
"distance" : {
"text" : "6.7 mi",
"value" : 10728
},
"duration" : {
"text" : "22 mins",
"value" : 1327
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}

我最终想得到一个列表(在 dart 中),它只是元素数组中距离“文本”值的列表,但我无法让它返回。我已经尝试创建一个类并将 json 结果映射到这个但是没有成功,因为我不是很擅长解析 json,所以任何关于如何结束这个列表的建议都将不胜感激!

我试过这段代码来解析 json,但我真的很难让它工作然后应用它:

class Topleveladd {
final String elements;

Topleveladd({this.elements});

factory Topleveladd.fromJson(Map<String, dynamic> parsedJson) {
return Topleveladd(elements: parsedJson['rows']);
}
}

class Elements {
List<Distance> distanceslist;

Elements({this.distanceslist});

factory Elements.fromJson(Map<String, dynamic> parsedJson) {
var list = parsedJson['elements'] as List;
print(list.runtimeType); //returns List<dynamic>
List<Distance> distancesList =
list.map((i) => Distance.fromJson(i)).toList();
return Elements(distanceslist: distancesList);
}
}

class Distance {
String text;
Distance({this.text});

factory Distance.fromJson(Map<String, dynamic> parsedJson) {
return new Distance(
text: parsedJson['distance'],
);
}
}

最佳答案

好的,这就是我访问您作为 Assets 提供的 JSON 的工作,因此您可能必须更改 loadData 方法才能满足您的需求。

距离矩阵类:

import 'dart:convert';
import 'dart:async' show Future;
import 'package:flutter/services.dart' show rootBundle;

class DistanceMatrix {
final List<String> destinations;
final List<String> origins;
final List<Element> elements;
final String status;

DistanceMatrix({this.destinations, this.origins, this.elements, this.status});

factory DistanceMatrix.fromJson(Map<String, dynamic> json) {
var destinationsJson = json['destination_addresses'];
var originsJson = json['origin_addresses'];
var rowsJson = json['rows'][0]['elements'] as List;

return DistanceMatrix(
destinations: destinationsJson.cast<String>(),
origins: originsJson.cast<String>(),
elements: rowsJson.map((i) => new Element.fromJson(i)).toList(),
status: json['status']);
}

static Future<DistanceMatrix> loadData() async {
DistanceMatrix distanceMatrix;
try{
String jsonData = await rootBundle.loadString('assets/data.json');
distanceMatrix = new DistanceMatrix.fromJson(json.decode(jsonData));
} catch (e){
print(e);
}
return distanceMatrix;
}
}

class Element {
final Distance distance;
final Duration duration;
final String status;

Element({this.distance, this.duration, this.status});

factory Element.fromJson(Map<String, dynamic> json) {
return Element(
distance: new Distance.fromJson(json['distance']),
duration: new Duration.fromJson(json['duration']),
status: json['status']);
}
}

class Distance {
final String text;
final int value;

Distance({this.text, this.value});

factory Distance.fromJson(Map<String, dynamic> json) {
return new Distance(text: json['text'], value: json['value']);
}
}

class Duration {
final String text;
final int value;

Duration({this.text, this.value});

factory Duration.fromJson(Map<String, dynamic> json) {
return new Duration(text: json['text'], value: json['value']);
}
}

Main.dart 使用 ListView.builder 将距离文本和值显示为 ListTile:

import 'package:flutter/material.dart';
import 'package:hello_world/distance_matrix.dart';

void main() async {
runApp(new MyApp(
distanceMatrix: await DistanceMatrix.loadData(),
));
}

class MyApp extends StatefulWidget {
final DistanceMatrix distanceMatrix;

@override
_MyAppState createState() => new _MyAppState();

MyApp({this.distanceMatrix});
}

class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Home"),
),
body: Material(
child: ListView.builder(
itemCount: widget.distanceMatrix.elements.length,
itemBuilder: (context, index){
return ListTile(
title: Text(widget.distanceMatrix.elements[index].distance.text),
subtitle: Text(widget.distanceMatrix.elements[index].distance.value.toString()),
);
},
)
)));
}
}

图像显示你应该得到什么:

enter image description here

关于json - Flutter与Distance Matrix API解析json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53485071/

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