如何解析嵌套JSON
我能够使用以下代码解析JSON如何解析嵌套JSON,json,flutter,dart,Json,Flutter,Dart,我能够使用以下代码解析JSON Map<String, dynamic> map = jsonDecode(response.body); // import 'dart:convert'; List<dynamic> datalist = map['data']; ** 我的JSON的dart代码 class ProductList { int status; String message; List<Data&g
Map<String, dynamic> map = jsonDecode(response.body); // import 'dart:convert';
List<dynamic> datalist = map['data'];
**
我的JSON的dart代码
class ProductList {
int status;
String message;
List<Data> data;
ProductList({this.status, this.message, this.data});
ProductList.fromJson(Map<String, dynamic> json) {
status = json['status'];
message = json['message'];
if (json['data'] != null) {
data = new List<Data>();
json['data'].forEach((v) {
data.add(new Data.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['status'] = this.status;
data['message'] = this.message;
if (this.data != null) {
data['data'] = this.data.map((v) => v.toJson()).toList();
}
return data;
}
}
class Data {
String id;
String productName;
String qty;
String unit;
String productImg;
String description;
String salePrice;
String donationAmt;
Data(
{this.id,
this.productName,
this.qty,
this.unit,
this.productImg,
this.description,
this.salePrice,
this.donationAmt});
Data.fromJson(Map<String, dynamic> json) {
id = json['id'];
productName = json['product_name'];
qty = json['qty'];
unit = json['unit'];
productImg = json['product_img'];
description = json['description'];
salePrice = json['sale_price'];
donationAmt = json['donation_amt'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['product_name'] = this.productName;
data['qty'] = this.qty;
data['unit'] = this.unit;
data['product_img'] = this.productImg;
data['description'] = this.description;
data['sale_price'] = this.salePrice;
data['donation_amt'] = this.donationAmt;
return data;
}
}
类产品列表{
智力状态;
字符串消息;
列出数据;
ProductList({this.status,this.message,this.data});
ProductList.fromJson(映射json){
status=json['status'];
message=json['message'];
if(json['data']!=null){
数据=新列表();
json['data'].forEach((v){
data.add(new data.fromJson(v));
});
}
}
映射到JSON(){
最终地图数据=新地图();
数据['status']=this.status;
数据['message']=此.message;
如果(this.data!=null){
data['data']=this.data.map((v)=>v.toJson()).toList();
}
返回数据;
}
}
类数据{
字符串id;
字符串名称;
字符串数量;
弦单元;
字符串生成;
字符串描述;
字符串销售价格;
字符串捐赠金额;
资料(
{this.id,
此.productName,
这个数量,
这个单位,,
这个产品,
这个.说明,,
这个价格,
这是一个名为.donationAmt});
Data.fromJson(映射json){
id=json['id'];
productName=json[“产品名称”];
qty=json['qty'];
unit=json['unit'];
productImg=json['product_img'];
description=json['description'];
salePrice=json['sale_price'];
donationAmt=json[“捐赠金额”];
}
映射到JSON(){
最终地图数据=新地图();
数据['id']=this.id;
数据['product\u name']=this.productName;
数据['qty']=此数量;
数据['unit']=这个.unit;
数据['product_img']=this.productImg;
数据['description']=this.description;
数据['sale_price']=此.salePrice;
数据['donation\u amt']=this.donationAmt;
返回数据;
}
}
以下是下拉列表的代码。我们需要用产品名称和id填充下拉列表。产品名称和id字段位于JSON的数据部分
Padding(
padding: const EdgeInsets.fromLTRB(25.0, 20.0, 0, 0),
child: Container(
width: 160,
height: 40,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5.0),
border: Border.all(
color: Colors.red,
style: BorderStyle.solid,
width: 0.80),
),
child: DropdownButton<Product>(
value: selectedUser,
icon: Padding(
padding: const EdgeInsets.only(left:15.0),
child: Icon(Icons.arrow_drop_down),
),
iconSize: 25,
underline: SizedBox(),
onChanged: (Product newValue) {
setState(() {
selectedUser = newValue;
});
},
items: users.map((Product user) {
return DropdownMenuItem<Product>(
value: user,
child: Padding(
padding:
const EdgeInsets.only(left: 10.0),
child: Text(
user.name,
style: TextStyle(
fontSize: 18,
color: Colors.black,
),
),
),
);
}).toList()),
),
),
填充(
填充:LTRB(25.0,20.0,0,0)中的常量边集,
子:容器(
宽度:160,
身高:40,
装饰:盒子装饰(
边界半径:边界半径。圆形(5.0),
边界:边界(
颜色:颜色,红色,
样式:BorderStyle.solid,
宽度:0.80),
),
孩子:下拉按钮(
值:selectedUser,
图标:填充(
填充:仅限常量边集(左:15.0),
子:图标(图标。箭头下拉),
),
iconSize:25,
下划线:SizedBox(),
变更后:(产品新价值){
设置状态(){
selectedUser=newValue;
});
},
项目:users.map((产品用户){
返回下拉菜单项(
值:用户,
孩子:填充(
衬垫:
仅限常数边集(左:10.0),
子:文本(
user.name,
样式:TextStyle(
尺码:18,
颜色:颜色,黑色,
),
),
),
);
}).toList()),
),
),
请记住:“JSON不是“嵌套的”。“当您得到一个JSON字符串时,您将对其进行解码,这将为您提供一个数据结构。。。这很可能是“嵌套的”。如何正确处理这一点取决于您。。。我们不是一个代码编写服务
始终将JSON(或YAML或XML)视为“黑盒”。使用该语言中提供的实用程序对其进行编码、解码和解析。正如您所述,我做了一些更改,并在本地加载了JSON,您可以进行api调用,然后一切都一样:
{
"status": 0,
"message": "Product Not Found",
"data": [
{
"id": "1",
"product_name": "Pet 0.5",
"qty": "500",
"unit": "ml",
"product_img": "SRC.jpg",
"description": "sgsdgdfhdfhh",
"sale_price": "100",
"donation_amt": "10"
},
{
"id": "7",
"product_name": "Pet 1l",
"qty": "1",
"unit": "l",
"product_img": "SRC1.jpg",
"description": "dgdg",
"sale_price": "20",
"donation_amt": "1"
}
]
}
您提供的json
// To parse this JSON data, do
//
// final productList = productListFromJson(jsonString);
import 'dart:convert';
ProductList productListFromJson(String str) =>
ProductList.fromJson(json.decode(str));
String productListToJson(ProductList data) => json.encode(data.toJson());
class ProductList {
int status;
String message;
List<Datum> data;
ProductList({
this.status,
this.message,
this.data,
});
factory ProductList.fromJson(Map<String, dynamic> json) => ProductList(
status: json["status"],
message: json["message"],
data: List<Datum>.from(json["data"].map((x) => Datum.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"status": status,
"message": message,
"data": List<dynamic>.from(data.map((x) => x.toJson())),
};
}
class Datum {
String id;
String productName;
String qty;
String unit;
String productImg;
String description;
String salePrice;
String donationAmt;
Datum({
this.id,
this.productName,
this.qty,
this.unit,
this.productImg,
this.description,
this.salePrice,
this.donationAmt,
});
factory Datum.fromJson(Map<String, dynamic> json) => Datum(
id: json["id"],
productName: json["product_name"],
qty: json["qty"],
unit: json["unit"],
productImg: json["product_img"],
description: json["description"],
salePrice: json["sale_price"],
donationAmt: json["donation_amt"],
);
Map<String, dynamic> toJson() => {
"id": id,
"product_name": productName,
"qty": qty,
"unit": unit,
"product_img": productImg,
"description": description,
"sale_price": salePrice,
"donation_amt": donationAmt,
};
}
//要解析此JSON数据,请执行以下操作
//
//最终产品列表=productListFromJson(jsonString);
导入“dart:convert”;
ProductList productListFromJson(字符串str)=>
ProductList.fromJson(json.decode(str));
字符串productListToJson(ProductList数据)=>json.encode(data.toJson());
类产品列表{
智力状态;
字符串消息;
列出数据;
产品列表({
这个,身份,,
这条信息,
这个数据,,
});
factory ProductList.fromJson(映射json)=>ProductList(
状态:json[“状态”],
message:json[“message”],
数据:List.from(json[“data”].map((x)=>Datum.fromJson(x)),
);
映射到JSON()=>{
“地位”:地位,
“信息”:信息,
“data”:List.from(data.map((x)=>x.toJson()),
};
}
类基准{
字符串id;
字符串名称;
字符串数量;
弦单元;
字符串生成;
字符串描述;
字符串销售价格;
字符串捐赠金额;
基准面({
这个身份证,
此.productName,
这个数量,
这个单位,,
这个产品,
这个.说明,,
这个价格,
这是捐赠金额,
});
工厂数据.fromJson(映射json)=>数据(
id:json[“id”],
产品名称:json[“产品名称”],
数量:json[“数量”],
单位:json[“单位”],
productImg:json[“product\u img”],
description:json[“description”],
salePrice:json[“销售价格”],
// To parse this JSON data, do
//
// final productList = productListFromJson(jsonString);
import 'dart:convert';
ProductList productListFromJson(String str) =>
ProductList.fromJson(json.decode(str));
String productListToJson(ProductList data) => json.encode(data.toJson());
class ProductList {
int status;
String message;
List<Datum> data;
ProductList({
this.status,
this.message,
this.data,
});
factory ProductList.fromJson(Map<String, dynamic> json) => ProductList(
status: json["status"],
message: json["message"],
data: List<Datum>.from(json["data"].map((x) => Datum.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"status": status,
"message": message,
"data": List<dynamic>.from(data.map((x) => x.toJson())),
};
}
class Datum {
String id;
String productName;
String qty;
String unit;
String productImg;
String description;
String salePrice;
String donationAmt;
Datum({
this.id,
this.productName,
this.qty,
this.unit,
this.productImg,
this.description,
this.salePrice,
this.donationAmt,
});
factory Datum.fromJson(Map<String, dynamic> json) => Datum(
id: json["id"],
productName: json["product_name"],
qty: json["qty"],
unit: json["unit"],
productImg: json["product_img"],
description: json["description"],
salePrice: json["sale_price"],
donationAmt: json["donation_amt"],
);
Map<String, dynamic> toJson() => {
"id": id,
"product_name": productName,
"qty": qty,
"unit": unit,
"product_img": productImg,
"description": description,
"sale_price": salePrice,
"donation_amt": donationAmt,
};
}
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:sample_testing_project/models.dart';
main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _currentSelectedValue;
List<Datum> data = List();
bool _isLoading = false;
String selectedUser;
@override
void initState() {
// TODO: implement initState
super.initState();
loadYourData();
}
Future<String> loadFromAssets() async {
return await rootBundle.loadString('json/parse.json');
}
loadYourData() async {
setState(() {
_isLoading = true;
});
// Loading your json locally you can make an api call, when you get the response just pass it to the productListFromJson method
String jsonString = await loadFromAssets();
final productList = productListFromJson(jsonString);
data = productList.data;
setState(() {
_isLoading = false;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: _isLoading
? Text('Loading')
: Container(
child: Padding(
padding: const EdgeInsets.fromLTRB(25.0, 20.0, 0, 0),
child: Container(
width: 160,
height: 40,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5.0),
border: Border.all(
color: Colors.red,
style: BorderStyle.solid,
width: 0.80),
),
child: DropdownButton(
value: selectedUser,
isExpanded: true,
icon: Padding(
padding: const EdgeInsets.only(left: 15.0),
child: Icon(Icons.arrow_drop_down),
),
iconSize: 25,
underline: SizedBox(),
onChanged: (newValue) {
setState(() {
selectedUser = newValue;
});
},
hint: Padding(
padding: const EdgeInsets.all(8.0),
child: Text('Select'),
),
items: data.map((data) {
return DropdownMenuItem(
value: data.id,
child: Padding(
padding: const EdgeInsets.only(left: 10.0),
child: Text(
data.id + ':' + data.productName,
style: TextStyle(
fontSize: 18,
color: Colors.black,
),
),
),
);
}).toList()),
),
),
),
),
);
}
}