从Json文件加载和读取数据 [英] Load and read data from Json file
本文介绍了从Json文件加载和读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个简单的应用程序,它从json文件中获取标记数据并绘制它们。
下面的代码是从 data.json
获取数据并将其添加到标记列表中。
I create simple app get data of marker from json file and draw them.
The code below is get data from data.json
and add to the list of marker.
我遇到了问题。我无法从json文件中获取数据并将其添加到标记列表中。我该怎么办?
I got problem. I can't get data from json file and add it to my marker list. How can I do that?
我的 main.dart
代码
class MapSample extends StatefulWidget {
@override
State<MapSample> createState() => MapSampleState();
}
class MapSampleState extends State<MapSample> {
@override
void initState() {
var location = Location();
FutureBuilder(
future:
DefaultAssetBundle.of(context).loadString('assets/data.json'),
builder: (context, snapshot) {
// Decode the JSON
var new_data = json.decode(snapshot.data.toString());
for (var i = 0; i < new_data.length; i++) {
location = Location(
id: new_data[i]['id'],
lat: new_data[i]['x'],
long: new_data[i]['y'],
);
locations.add(location);
//print(location.lat);
}
});
super.initState();
}
}
我的 data.json
[{
"rownum": 1,
"id": "E3E0D2C5-CB82-4AF3-8D5D-4CD323560F59",
"x": 10.99803453,
"y": 106.65676933,
}, {
"rownum": 2,
"id": "5FFB6736-7D1F-4B40-A397-32EB3128BC30",
"x": 10.99793271,
"y": 106.65666751,
},
推荐答案
尝试一下。
-
您必须以assest的方式添加data.json文件。
You have to add the data.json file in assests.
然后将其添加到pubspec.yaml文件中。
Then add in to pubspec.yaml file.
assets:
- assets/data.json
- 然后在下面的代码中添加。 / li>
- Then add this below code.
String jsonData = await DefaultAssetBundle.of(context).loadString("assets/data.json");
final jsonResult = json.decode(jsonData);
这篇关于从Json文件加载和读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文