Flutter Json对象和数组解析成实体类

本文介绍了如何使用Dart语言解析JSON格式的数据,包括解析JSON对象和JSON数组的方法,并提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.解析json对象:

准备一个json 对象数据:

String json="{"res": true, "age": 12, "Name":"张三"}";

使用JSON to Dart 工具自动生成实体类,也可以自己手动写。

工具地址:JSON to Dart

 将代码复制到类当中:

 

下面解析字符串:

Map<String, dynamic> data= convert.jsonDecode(json);

调用实体类:

jsonObject result=jsonObject.fromJson(data);

从实体类取值:

var name=result.name;

2.解析json数组:

准备一个json数组数据:

String jsonList="{ "User": [{ "age": 12, "Name":"张三"},{ "age": 24, "Name":"李四"}]}";

先以同样的方法创建一个数组里面一个对象的实体类。

 

 解析Json数组:

Map<String, dynamic> data = convert.jsonDecode(json);

//可以通过解析数组
// List JsonList = convert.jsonDecode(list);// 注:list 是数组的字符串

List listData = data["User"];//获取数组
//循环数组插入到对象集合
List<jsonObject> listVos =listData.map((item) => jsonObject.fromJson(item)).toList(); //获取第一组数组里面name
var name = listVos[0].name;

json 对象和数组的解析和使用就介绍到这里。

{ "code": 200, "msg": "操作功", "data": { "id": 1, "tplName": "参数录入表单模板", "tplCode": "paramsTpl", "tplContent": "[{\"modelName\":\"蒸汽参数\",\"modelCode\":\"stream\",\"modelTitleColor\":\"#006FFF\",\"modelTitleIcon\":\"../images/zq.png\",\"moduleList\":[{\"label\":\"蒸汽流量\",\"unit\":\"t/h\",\"value\":\"\",\"placeholder\":\"请输入蒸汽流量\",\"type\":\"input\",\"code\":\"Ded\",\"options\":\"\",\"inputType\":\"number\"},{\"label\":\"蒸汽温度\",\"unit\":\"℃\",\"value\":\"\",\"placeholder\":\"请输入蒸汽温度\",\"type\":\"input\",\"code\":\"Ted\",\"options\":\"\",\"inputType\":\"number\"},{\"label\":\"蒸汽压力\",\"unit\":\"Mpa\",\"value\":\"\",\"placeholder\":\"请输入蒸汽压力\",\"type\":\"input\",\"code\":\"Ped\",\"options\":\"\",\"inputType\":\"number\"}]},{\"modelName\":\"水参数\",\"modelCode\":\"stream1\",\"modelTitleColor\":\"#3AB589\",\"modelTitleIcon\":\"../images/water.png\",\"moduleList\":[{\"label\":\"给水温度\",\"unit\":\"℃\",\"value\":\"\",\"placeholder\":\"请输入给水温度\",\"type\":\"input\",\"code\":\"Tgs\",\"options\":\"\",\"inputType\":\"number\"},{\"label\":\"给水压力\",\"unit\":\"Mpa\",\"value\":\"\",\"placeholder\":\"请输入给水压力\",\"type\":\"input\",\"code\":\"Pgs\",\"options\":\"\",\"inputType\":\"number\"},{\"label\":\"冷凝水回收量\",\"unit\":\"t/h\",\"value\":\"\",\"placeholder\":\"请输入冷凝水回收量\",\"type\":\"input\",\"code\":\"D_ed\",\"options\":\"\",\"inputType\":\"number\"}]},{\"modelName\":\"供汽参数\",\"modelCode\":\"stream2\",\"modelTitleColor\":\"#EA8432\",\"modelTitleIcon\":\"../images/gq.png\",\"moduleList\":[{\"label\":\"供汽时长\",\"unit\":\"h/d\",\"value\":\"\",\"placeholder\":\"请输入供汽时长\",\"type\":\"input\",\"code\":\"Hed\",\"options\":\"\",\"inputType\":\"number\"},{\"label\":\"日供汽量\",\"unit\":\"t/d\",\"value\":\"\",\"placeholder\":\"请输入日供汽量\",\"type\":\"input\",\"code\":\"Sed\",\"options\":\"\",\"inputType\":\"number\"},{\"label\":\"年工作时间\",\"unit\":\"d/y\",\"value\":\"\",\"placeholder\":\"请输入年工作时间\",\"type\":\"input\",\"code\":\"Day\",\"options\":\"\",\"inputType\":\"number\"},{\"label\":\"年供汽量\",\"unit\":\"t/y\",\"value\":\"\",\"placeholder\":\"请输入年供汽量\",\"type\":\"input\",\"code\":\"S_ed\",\"options\":\"\",\"inputType\":\"number\"}]},{\"modelName\":\"电价参数\",\"modelCode\":\"stream3\",\"modelTitleColor\":\"#42B51A\",\"modelTitleIcon\":\"../images/dj.png\",\"moduleList\":[{\"label\":\"天然气单价\",\"unit\":\"元/m³\",\"value\":\"\",\"placeholder\":\"请输入天然气单价\",\"type\":\"input\",\"code\":\"Vng\",\"options\":\"\",\"inputType\":\"number\"},{\"label\":\"谷电价格\",\"unit\":\"元/kWh\",\"value\":\"\",\"placeholder\":\"请输入谷电价格\",\"type\":\"input\",\"code\":\"Vve\",\"options\":\"\",\"inputType\":\"number\"},{\"label\":\"谷电时长\",\"unit\":\"h\",\"value\":\"\",\"placeholder\":\"请输入谷电时长\",\"type\":\"input\",\"code\":\"Hrv\",\"options\":\"\",\"inputType\":\"number\"},{\"label\":\"平电价格\",\"unit\":\"元/kWh\",\"value\":\"\",\"placeholder\":\"请输入平电价格\",\"type\":\"input\",\"code\":\"Vhe\",\"options\":\"\",\"inputType\":\"number\"},{\"label\":\"平电时长\",\"unit\":\"h\",\"value\":\"\",\"placeholder\":\"请输入平电时长\",\"type\":\"input\",\"code\":\"Hhe\",\"options\":\"\",\"inputType\":\"number\"},{\"label\":\"峰电价格\",\"unit\":\"元/kWh\",\"value\":\"\",\"placeholder\":\"请输入峰电价格\",\"type\":\"input\",\"code\":\"Vpe\",\"options\":\"\",\"inputType\":\"number\"},{\"label\":\"峰电时长\",\"unit\":\"h\",\"value\":\"\",\"placeholder\":\"请输入峰电时长\",\"type\":\"input\",\"code\":\"Hpe\",\"options\":\"\",\"inputType\":\"number\"},{\"label\":\"是否需要增容\",\"unit\":\"\",\"value\":\"\",\"placeholder\":\"\",\"type\":\"select\",\"code\":\"add_capacity\",\"options\":[{\"name\":\"是\",\"value\":\"1\"},{\"name\":\"否\",\"value\":\"0\"}],\"inputType\":\"\",\"relationship\":{\"relationshipValue\":\"0\",\"relationshipField\":\"item\",\"relationshipSource\":[{\"parentCode\":\"storage\",\"selfCode\":[\"LCC_se10\",\"LCC_se20\"]},{\"parentCode\":\"magnesiaBrik\",\"selfCode\":[\"LCC_mb10\",\"LCC_mb20\"]},{\"parentCode\":\"siact\",\"selfCode\":[\"ICC\",\"LCC_10\",\"LCC_20\"]}]}},{\"label\":\"容量电费\",\"unit\":\"元/kVA\",\"value\":\"\",\"placeholder\":\"请输入容量电费\",\"type\":\"input\",\"code\":\"Vcc\",\"options\":\"\",\"inputType\":\"number\"},{\"label\":\"需量电费\",\"unit\":\"元/kVA\",\"value\":\"\",\"placeholder\":\"请输入需量电费\",\"type\":\"input\",\"code\":\"Vdc\",\"options\":\"\",\"inputType\":\"number\"},{\"label\":\"水价格\",\"unit\":\"元/t\",\"value\":\"\",\"placeholder\":\"请输入水价格\",\"type\":\"input\",\"code\":\"Ved\",\"options\":\"\",\"inputType\":\"number\"},{\"label\":\"蒸汽售出价格\",\"unit\":\"元/t\",\"value\":\"\",\"placeholder\":\"请输入蒸汽售出价格\",\"type\":\"input\",\"code\":\"Vst\",\"options\":\"\",\"inputType\":\"number\"}]},{\"modelName\":\"热参数\",\"modelCode\":\"stream4\",\"modelTitleColor\":\"#EA8432\",\"modelTitleIcon\":\"../images/hot.png\",\"moduleList\":[{\"label\":\"储热时长\",\"unit\":\"h\",\"value\":\"\",\"placeholder\":\"请输入储热时长\",\"type\":\"input\",\"code\":\"Htc\",\"options\":\"\",\"inputType\":\"number\"},{\"label\":\"放热时长\",\"unit\":\"h\",\"value\":\"\",\"placeholder\":\"请输入放热时长\",\"type\":\"input\",\"code\":\"Htd\",\"options\":\"\",\"inputType\":\"number\"}]}]", "tplType": null, "tplDescribe": "参数录入表单模板", "createTime": null } } 使用flutter进行换为实体类
最新发布
05-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值