gpt4 book ai didi

json - 当响应是对象数组时,如何在抖动中解析JSON

转载 作者:行者123 更新时间:2023-12-03 04:10:59 25 4
gpt4 key购买 nike

我收到以下类型的API响应

[
{"name":"lakshay","id":"1217","type":"Staff","temperature":56.6,"date":"12-06-2020"},
{"name":"lakshay","id":"1217","type":"Staff","temperature":56.6,"date":"12-06-2020"},
.....
]

我需要将整个内容解析为对象列表,以便可以执行类似的操作
// conceptual //
array.map((obj)=>{
// computation //
})

我从 here那里获得了帮助,但是它没有告诉我们如何解析外部数组。

谢谢你的时间。

最佳答案

您可以在下面复制粘贴运行完整代码
第1步:使用List<Payload> payloadList = payloadFromJson(jsonString);进行解析,您可以看到完整的代码以获取详细的Payload类定义
步骤2:进行这样的循环

payloadList.forEach((element) {
print(element.name);
print(element.id);
});

输出
I/flutter (17660): lakshay
I/flutter (17660): 1217
I/flutter (17660): lakshay
I/flutter (17660): 1217

完整的代码
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 {
Payload({
this.name,
this.id,
this.type,
this.temperature,
this.date,
});

String name;
String id;
String type;
double temperature;
String date;

factory Payload.fromJson(Map<String, dynamic> json) => Payload(
name: json["name"],
id: json["id"],
type: json["type"],
temperature: json["temperature"].toDouble(),
date: json["date"],
);

Map<String, dynamic> toJson() => {
"name": name,
"id": id,
"type": type,
"temperature": temperature,
"date": date,
};
}

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;
String jsonString = '''
[
{"name":"lakshay","id":"1217","type":"Staff","temperature":56.6,"date":"12-06-2020"},
{"name":"lakshay","id":"1217","type":"Staff","temperature":56.6,"date":"12-06-2020"}

]
''';

void _incrementCounter() {
List<Payload> payloadList = payloadFromJson(jsonString);
payloadList.forEach((element) {
print(element.name);
print(element.id);
});

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 - 当响应是对象数组时,如何在抖动中解析JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62338805/

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