由于不需要的键,无法遍历 JSON 对象
Cannot iterate through the JSON object because of the unwanted keys
我的JSON响应对象是这样的
{
"response": "success",
"message": "done",
"data": {
"orders": {
"order4": {
"OrderId": "4",
"OrderSubTotal": "568",
"Items": {
"1": {
"ProductName": "Prod 1",
"ItemDiscount": "10",
"Quantity": "3",
"TotalItemPrice": "161.865"
},
"2": {
"ProductName": "Prod 2",
"ItemDiscount": "0",
"Quantity": "5",
"TotalItemPrice": "449.75"
}
},
"order6": {
"OrderId": "6",
"total": "789",
"Items": {
"1": {
"ProductName": "Prod 1",
"ItemDiscount": "10",
"Quantity": "3",
"TotalItemPrice": "161.865"
},
"2": {
"ProductName": "Prod 2",
"ItemDiscount": "0",
"Quantity": "5",
"TotalItemPrice": "449.75"
}
}
}
}
}
}
}
问题是我无法遍历字段,因为我得到的是随机键,如 order6、order4。那么有没有办法从对象中删除这些键并创建一个漂亮干净的 JSON 对象。我用飞镖语言编码。
如果我知道你想要什么,那么 SplayTreeMap.from(map, comparator)
可以像下面的代码一样为你完成工作:
import 'dart:convert';
import 'dart:collection';
void main() {
String str = r'''{
"response": "success",
"message": "done",
"data": {
"orders": {
"order6": {
"OrderId": "6",
"total": "789",
"Items": {
"1": {
"ProductName": "Prod 1",
"ItemDiscount": "10",
"Quantity": "3",
"TotalItemPrice": "161.865"
},
"2": {
"ProductName": "Prod 2",
"ItemDiscount": "0",
"Quantity": "5",
"TotalItemPrice": "449.75"
}
}
},
"order4": {
"OrderId": "4",
"OrderSubTotal": "568",
"Items": {
"1": {
"ProductName": "Prod 1",
"ItemDiscount": "10",
"Quantity": "3",
"TotalItemPrice": "161.865"
},
"2": {
"ProductName": "Prod 2",
"ItemDiscount": "0",
"Quantity": "5",
"TotalItemPrice": "449.75"
}
}
}
}
}
}''';
final sortedMap = new SplayTreeMap<String,dynamic>.from(jsonDecode(str)["data"]["orders"], (a,b)=>a.compareTo(b));
print(sortedMap);
}
请查看我根据您提供的 json 制作的示例:
以下是您提供的样本 json:
{
"response": "success",
"message": "done",
"data": {
"orders": {
"order6": {
"OrderId": "6",
"total": "789",
"Items": {
"1": {
"ProductName": "Prod 1",
"ItemDiscount": "10",
"Quantity": "3",
"TotalItemPrice": "161.865"
},
"2": {
"ProductName": "Prod 2",
"ItemDiscount": "0",
"Quantity": "5",
"TotalItemPrice": "449.75"
}
}
},
"order4": {
"OrderId": "4",
"OrderSubTotal": "568",
"Items": {
"1": {
"ProductName": "Prod 1",
"ItemDiscount": "10",
"Quantity": "3",
"TotalItemPrice": "161.865"
},
"2": {
"ProductName": "Prod 2",
"ItemDiscount": "0",
"Quantity": "5",
"TotalItemPrice": "449.75"
}
}
}
}
}
}
基于json我做了一个ui给你显示订单详情:
import 'dart:convert';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(home: HomePage());
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
List<Order> ordersList = List();
@override
void initState() {
super.initState();
getData();
}
getData() async {
String data =
await DefaultAssetBundle.of(context).loadString("json/parse.json");
Map newMap = json.decode(data);
newMap['data'].forEach((k, v) {
v.forEach((key, val) {
String ordername = key;
List<Item> items = List();
val['Items'].forEach((key, value) {
print('sample');
Item item = Item(
productNumber: key,
productName: value['ProductName'],
itemDiscount: value['ItemDiscount'],
quantity: value['Quantity'],
totalItemPrice: value['TotalItemPrice'],
);
items.add(item);
print(key);
print(value);
print('length is :${items.length}');
});
Order order = Order(
orderId: val['OrderId'],
orderName: ordername,
total: val['total'],
items: items);
ordersList.add(order);
});
});
setState(() {});
print('This is the list : count ${ordersList.length}');
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: ListView.builder(
itemCount: ordersList.length,
shrinkWrap: true,
itemBuilder: (context, index) {
var order = ordersList[index];
return Card(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Order Name : ${order.orderName}'),
Text('Order Id : ${order.orderId}'),
Text('Order total : ${order.total}'),
Text('Order Items: ${order.items.length}')
],
),
);
}),
));
}
}
class Order {
final String orderName;
final String orderId;
final String total;
final List<Item> items;
Order({this.orderName, this.orderId, this.total, this.items});
}
class Item {
Item({
this.productName,
this.itemDiscount,
this.quantity,
this.totalItemPrice,
this.productNumber,
});
String productNumber;
String productName;
String itemDiscount;
String quantity;
String totalItemPrice;
}
也许我感到困惑的是您提供的 json 是否正确,请检查它是否在订单 4 中有 order6,并且有关于 OrderSubTotal 和 total 的关心参数,请告诉我是否有问题对了。
只需检查一下,如果有效请告诉我
我的JSON响应对象是这样的
{
"response": "success",
"message": "done",
"data": {
"orders": {
"order4": {
"OrderId": "4",
"OrderSubTotal": "568",
"Items": {
"1": {
"ProductName": "Prod 1",
"ItemDiscount": "10",
"Quantity": "3",
"TotalItemPrice": "161.865"
},
"2": {
"ProductName": "Prod 2",
"ItemDiscount": "0",
"Quantity": "5",
"TotalItemPrice": "449.75"
}
},
"order6": {
"OrderId": "6",
"total": "789",
"Items": {
"1": {
"ProductName": "Prod 1",
"ItemDiscount": "10",
"Quantity": "3",
"TotalItemPrice": "161.865"
},
"2": {
"ProductName": "Prod 2",
"ItemDiscount": "0",
"Quantity": "5",
"TotalItemPrice": "449.75"
}
}
}
}
}
}
}
问题是我无法遍历字段,因为我得到的是随机键,如 order6、order4。那么有没有办法从对象中删除这些键并创建一个漂亮干净的 JSON 对象。我用飞镖语言编码。
如果我知道你想要什么,那么 SplayTreeMap.from(map, comparator)
可以像下面的代码一样为你完成工作:
import 'dart:convert';
import 'dart:collection';
void main() {
String str = r'''{
"response": "success",
"message": "done",
"data": {
"orders": {
"order6": {
"OrderId": "6",
"total": "789",
"Items": {
"1": {
"ProductName": "Prod 1",
"ItemDiscount": "10",
"Quantity": "3",
"TotalItemPrice": "161.865"
},
"2": {
"ProductName": "Prod 2",
"ItemDiscount": "0",
"Quantity": "5",
"TotalItemPrice": "449.75"
}
}
},
"order4": {
"OrderId": "4",
"OrderSubTotal": "568",
"Items": {
"1": {
"ProductName": "Prod 1",
"ItemDiscount": "10",
"Quantity": "3",
"TotalItemPrice": "161.865"
},
"2": {
"ProductName": "Prod 2",
"ItemDiscount": "0",
"Quantity": "5",
"TotalItemPrice": "449.75"
}
}
}
}
}
}''';
final sortedMap = new SplayTreeMap<String,dynamic>.from(jsonDecode(str)["data"]["orders"], (a,b)=>a.compareTo(b));
print(sortedMap);
}
请查看我根据您提供的 json 制作的示例:
以下是您提供的样本 json:
{
"response": "success",
"message": "done",
"data": {
"orders": {
"order6": {
"OrderId": "6",
"total": "789",
"Items": {
"1": {
"ProductName": "Prod 1",
"ItemDiscount": "10",
"Quantity": "3",
"TotalItemPrice": "161.865"
},
"2": {
"ProductName": "Prod 2",
"ItemDiscount": "0",
"Quantity": "5",
"TotalItemPrice": "449.75"
}
}
},
"order4": {
"OrderId": "4",
"OrderSubTotal": "568",
"Items": {
"1": {
"ProductName": "Prod 1",
"ItemDiscount": "10",
"Quantity": "3",
"TotalItemPrice": "161.865"
},
"2": {
"ProductName": "Prod 2",
"ItemDiscount": "0",
"Quantity": "5",
"TotalItemPrice": "449.75"
}
}
}
}
}
}
基于json我做了一个ui给你显示订单详情:
import 'dart:convert';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(home: HomePage());
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
List<Order> ordersList = List();
@override
void initState() {
super.initState();
getData();
}
getData() async {
String data =
await DefaultAssetBundle.of(context).loadString("json/parse.json");
Map newMap = json.decode(data);
newMap['data'].forEach((k, v) {
v.forEach((key, val) {
String ordername = key;
List<Item> items = List();
val['Items'].forEach((key, value) {
print('sample');
Item item = Item(
productNumber: key,
productName: value['ProductName'],
itemDiscount: value['ItemDiscount'],
quantity: value['Quantity'],
totalItemPrice: value['TotalItemPrice'],
);
items.add(item);
print(key);
print(value);
print('length is :${items.length}');
});
Order order = Order(
orderId: val['OrderId'],
orderName: ordername,
total: val['total'],
items: items);
ordersList.add(order);
});
});
setState(() {});
print('This is the list : count ${ordersList.length}');
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: ListView.builder(
itemCount: ordersList.length,
shrinkWrap: true,
itemBuilder: (context, index) {
var order = ordersList[index];
return Card(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Order Name : ${order.orderName}'),
Text('Order Id : ${order.orderId}'),
Text('Order total : ${order.total}'),
Text('Order Items: ${order.items.length}')
],
),
);
}),
));
}
}
class Order {
final String orderName;
final String orderId;
final String total;
final List<Item> items;
Order({this.orderName, this.orderId, this.total, this.items});
}
class Item {
Item({
this.productName,
this.itemDiscount,
this.quantity,
this.totalItemPrice,
this.productNumber,
});
String productNumber;
String productName;
String itemDiscount;
String quantity;
String totalItemPrice;
}
也许我感到困惑的是您提供的 json 是否正确,请检查它是否在订单 4 中有 order6,并且有关于 OrderSubTotal 和 total 的关心参数,请告诉我是否有问题对了。
只需检查一下,如果有效请告诉我