如何访问 Json 数据
How to access Json data's
我有一些 Json 数据。但是如果不写第一个键名我就无法访问它的内部。
Json 数据的 link 是 https://api.genelpara.com/embed/borsa.json 我想把它们放到 Listview 中。我该如何放置它们?如果我像代码一样编写,我只能访问 'satis' 键和值并在 Listview 上显示,但我想访问 'ALGYO' 键作为值并在 Listview 上显示。
child: Center(child: Text(' ${snapshot.data['ALGYO']['satis']}'))
您需要从您的 JSON 中获取 keys
(例如 ALGYO
和 ARZUM
),然后您可以访问其中的数据。试试下面的代码,你可以轻松地在你想要的地方添加alis
和degisim
:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'ListView',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final data = {
'ALGYO': {'satis': "20.18", 'alis': "20.16", 'degisim': "-1,46"},
'ARZUM': {'satis': "17.68", 'alis': "17.68", 'degisim': "-20,93"},
'ACSEL': {'satis': "16.41", 'alis': "16.41", 'degisim': "-2,32"},
'ADANA': {'satis': "12.19", 'alis': "12.19", 'degisim': "0,00"},
'ADBGR': {'satis': "8.62", 'alis': "8.62", 'degisim': "0,00"},
'ADEL': {'satis': "0.0476", 'alis': "0.0476", 'degisim': "-1,04"},
'ADESE': {'satis': "1.2", 'alis': "1.2", 'degisim': "-1,64"},
'ADNAC': {'satis': "2.09", 'alis': "2.09", 'degisim': "0,00"},
'AEFES': {'satis': "21.46", 'alis': "21.46", 'degisim': "-1,29"},
'AFYON': {'satis': "3.63", 'alis': "3.63", 'degisim': "-0,82"},
};
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('ListView'),
),
body: ListView.builder(
itemCount: data.length,
itemBuilder: (BuildContext context, int index) {
// this will be the key for the current item
String key = data.keys.elementAt(index);
// here you can see how to access "satis" under
// current key
return ListTile(
title: Text("$key"), subtitle: Text(data[key]!["satis"]!));
},
),
);
}
}
我有一些 Json 数据。但是如果不写第一个键名我就无法访问它的内部。 Json 数据的 link 是 https://api.genelpara.com/embed/borsa.json 我想把它们放到 Listview 中。我该如何放置它们?如果我像代码一样编写,我只能访问 'satis' 键和值并在 Listview 上显示,但我想访问 'ALGYO' 键作为值并在 Listview 上显示。
child: Center(child: Text(' ${snapshot.data['ALGYO']['satis']}'))
您需要从您的 JSON 中获取 keys
(例如 ALGYO
和 ARZUM
),然后您可以访问其中的数据。试试下面的代码,你可以轻松地在你想要的地方添加alis
和degisim
:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'ListView',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final data = {
'ALGYO': {'satis': "20.18", 'alis': "20.16", 'degisim': "-1,46"},
'ARZUM': {'satis': "17.68", 'alis': "17.68", 'degisim': "-20,93"},
'ACSEL': {'satis': "16.41", 'alis': "16.41", 'degisim': "-2,32"},
'ADANA': {'satis': "12.19", 'alis': "12.19", 'degisim': "0,00"},
'ADBGR': {'satis': "8.62", 'alis': "8.62", 'degisim': "0,00"},
'ADEL': {'satis': "0.0476", 'alis': "0.0476", 'degisim': "-1,04"},
'ADESE': {'satis': "1.2", 'alis': "1.2", 'degisim': "-1,64"},
'ADNAC': {'satis': "2.09", 'alis': "2.09", 'degisim': "0,00"},
'AEFES': {'satis': "21.46", 'alis': "21.46", 'degisim': "-1,29"},
'AFYON': {'satis': "3.63", 'alis': "3.63", 'degisim': "-0,82"},
};
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('ListView'),
),
body: ListView.builder(
itemCount: data.length,
itemBuilder: (BuildContext context, int index) {
// this will be the key for the current item
String key = data.keys.elementAt(index);
// here you can see how to access "satis" under
// current key
return ListTile(
title: Text("$key"), subtitle: Text(data[key]!["satis"]!));
},
),
);
}
}