在flatter中将JSON转换为ListView.builder
我是编程新手,所以请耐心等待我,我正在尝试将此JSON转换为类似此设计的显示方式, 我已经做了JSON解码,但对于示例JSON,所以我理解它是如何工作的,但对于复杂的JSON,我感到困惑,所以我非常感谢任何人的帮助 JSON代码:在flatter中将JSON转换为ListView.builder,json,flutter,android-listview,Json,Flutter,Android Listview,我是编程新手,所以请耐心等待我,我正在尝试将此JSON转换为类似此设计的显示方式, 我已经做了JSON解码,但对于示例JSON,所以我理解它是如何工作的,但对于复杂的JSON,我感到困惑,所以我非常感谢任何人的帮助 JSON代码: [ { "Category": "Gro", "stores": [ { "name": "market",
[
{
"Category": "Gro",
"stores": [
{
"name": "market",
"logo_url": "https://www.google.com/signpost-150x150.png",
"phone_1": "1111111111",
"phone_2": "1111111111",
"location": "https://maps.google.com/location"
},
{
"name": "mall",
"logo_url": "https://www.google.com/signpost-150x150.png",
"phone_1": "1111111111",
"phone_2": "1111111111",
"location": "https://maps.google.com/location"
}
]
},
{
"Category": "Food",
"stores": [
{
"name": "Food Time",
"logo_url": "https://www.google.com/signpost-150x150.png",
"phone_1": "1111111111",
"phone_2": "1111111111",
"location": "https://maps.google.com/location"
},
{
"name": "let's eat",
"logo_url": "https://www.google.com/signpost-150x150.png",
"phone_1": "1111111111",
"phone_2": "1111111111",
"location": "https://maps.google.com/location"
}
]
},
{
"Category": "Personal Care",
"stores": [
{
"name": "Body",
"logo_url": "https://www.google.com/signpost-150x150.png",
"phone_1": "1111111111",
"phone_2": "1111111111",
"location": "https://maps.google.com/location"
},
{
"name": "Hair",
"logo_url": "https://www.google.com/signpost-150x150.png",
"phone_1": "1111111111",
"phone_2": "1111111111",
"location": "https://maps.google.com/location"
}
]
}
]
据强强刮刮刮刮刮刮刮方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方###########
据强强刮刮刮刮刮刮刮方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方###########
这就是我想到的
import 'package:side_menu.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:http/http.dart';
import 'dart:convert';
Future<List> shopsList() async {
Response response = await get('JSON URL');
if (response.statusCode == 200) {
var shopsData = jsonDecode(response.body);
return shopsData;
} else {
print(response.statusCode);
}
}
class ShowShops extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
drawer: SideMenu(),
appBar: AppBar(),
body: Column(
children: [
Expanded(
child: Container(
child: FutureBuilder(
future: shopsList(),
builder: (context, shops) {
if (shops.hasData) {
return ListView.builder(
itemCount: shops.data.length,
itemBuilder: (BuildContext context, int index) {
Map shopInfo = shops.data[index];
String cat = shopInfo[index]['Category'];
return Card(
child: Text(cat),
);
},
);
}
return Center(
child: SpinKitWave(
color: Color(0xff023246),
size: 100,
),
//CircularProgressIndicator(),
);
}),
),
)
],
),
);
}
}
import'package:side_menu.dart';
进口“包装:颤振/cupertino.dart”;
进口“包装:颤振/材料.省道”;
进口“包装:颤振旋转套件/颤振旋转套件.dart”;
导入“包:http/http.dart”;
导入“dart:convert”;
Future shopsList()异步{
Response=await-get('JSON-URL');
如果(response.statusCode==200){
var shopsData=jsonDecode(response.body);
返回商店数据;
}否则{
打印(响应状态码);
}
}
类ShowShops扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回脚手架(
抽屉:侧菜单(),
appBar:appBar(),
正文:专栏(
儿童:[
扩大(
子:容器(
孩子:未来建设者(
future:shopsList(),
建筑商:(背景、商店){
if(shops.hasData){
返回ListView.builder(
itemCount:shops.data.length,
itemBuilder:(构建上下文,int索引){
Map shopInfo=shops.data[索引];
字符串cat=shopInfo[index]['Category'];
回程卡(
子:文本(cat),
);
},
);
}
返回中心(
孩子:斯宾基特波(
颜色:颜色(0xff023246),
尺码:100,
),
//CircularProgressIndicator(),
);
}),
),
)
],
),
);
}
}
但我遇到了错误,我不知道如何在类别下显示商店
提前感谢您的帮助添加
,
第9行
“phone_2”:“1111111111”
试试我的代码
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
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: Test(),
);
}
}
class Test extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: FutureBuilder(
future: http.get('https://dc-apps.net/map/services.json'),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
}
List data = json.decode(snapshot.data.body);
// print(data);
List categoriesnames = [];
List stores = [];
data.forEach((element) {
categoriesnames.add(element["Category"]);
stores.add(element['stores']);
});
// return Text('see');
return ListView.builder(
itemCount: data.length,
itemBuilder: (context, index) {
// print(stores[index][index]['name']);
return CardItem(
categoryname: categoriesnames[index],
sotories: stores[index],
);
},
);
},
),
);
}
}
class CardItem extends StatelessWidget {
final String categoryname;
List sotories;
CardItem({this.categoryname, this.sotories});
@override
Widget build(BuildContext context) {
return Container(
child: Expanded(
child: Column(
children: [
Text(categoryname),
SizedBox(
height: 5,
),
ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: sotories.length,
itemBuilder: (context, index) => ListTile(
title: Text(sotories[index]['name']),
subtitle: Column(
children: [
Text(sotories[index]['phone_1']),
Text(sotories[index]['phone_2']),
],
),
trailing: CircleAvatar(
radius: 50,
backgroundImage: NetworkImage(sotories[index]['logo_url']),
),
),
),
],
),
),
);
}
}
将JSON转换为对象列表并使用列表。