解码复杂的 JSON?

Posted

技术标签:

【中文标题】解码复杂的 JSON?【英文标题】:Decode complex JSON? 【发布时间】:2020-05-10 10:03:04 【问题描述】:

我想把这个复杂的 JSON 数据解码成普通的字符串,我怎么能用简单的方法呢?

【问题讨论】:

解码是什么意思??你在期待什么??到目前为止你尝试了什么?? 这能回答你的问题吗? How to decode JSON in Flutter? 我想把这个 JSON 数据放到 Flutter 项目中的 Card 视图中, 到目前为止你有没有尝试过? 是的,link,但在我的例子中,每个“earn”都有一个 details 对象 【参考方案1】:

更新

    List<Map> list = [];

      @override
      void initState() 
        super.initState();
        getList();
      

      void getList() async 
        //get data from internet/api
        //for ex. I m using offline data
        var jsonData = [
  
    "id": "1",
    "date": "18-Sep-2019",
    "paystub_Details": 
      "name": "Henry Nixon",
      "position": "Senior Programmer",
      "address": "30 Wertheim Crt",
      "suite": "Suite C16",
      "richmond": "Richmond Hill, ON L4B 1B9",
      "e_id": 21,
      "peroid": "10-Sep-2019 to 18-Sep-2019",
      "payment_date": "18-Sep-2019",
      "net_pay": 24600,
      "e_total": 38000,
      "d_total": 13400
    ,
    "earn": 
      "detail": [
        
          "description": "EI",
          "Units": 80,
          "Rate": 800,
          "Amount": 24000
        ,
        
          "description": "Hourly Wages",
          "Units": 80,
          "Rate": 800,
          "Amount": 14000
        
      ]
    ,
    "deduction": 
      "detail": [
        
          "description": "CPP/QPP",
          "Units": 2400
        ,
        
          "description": "EI",
          "Units": 1400
        ,
        
          "description": "Fed Inc Tax",
          "Units": 2400
        ,
        
          "description": "Prov Inc Taxi",
          "Units": 2400
        ,
        
          "description": "Cpp/Qpp",
          "Units": 2400
        ,
        
          "description": "Cpp/Qpp",
          "Units": 2400
        
      ]
    
  ,
  
    "id": "2",
    "date": "18-Oct-2019",
    "paystub_Details": 
      "name": "Naveen Avidi",
      "position": "The Programmer",
      "address": "30 Wertheim Crt",
      "suite": "Suite C16",
      "richmond": "Richmond Hill, ON L4B 1B9",
      "e_id": 21,
      "peroid": "10-Oct-2019 to 18-Oct-2019",
      "payment_date": "18-Oct-2019",
      "net_pay": 24600,
      "e_total": 38000,
      "d_total": 13400
    ,
    "earn": 
      "detail": [
        
          "description": "EI",
          "Units": 80,
          "Rate": 800,
          "Amount": 25000
        ,
        
          "description": "Hourly Wages",
          "Units": 80,
          "Rate": 800,
          "Amount": 17000
        
      ]
    ,
    "deduction": 
      "detail": [
        
          "description": "CPP/QPP",
          "Units": 2400
        ,
        
          "description": "EI",
          "Units": 1400
        ,
        
          "description": "Fed Inc Tax",
          "Units": 2400
        ,
        
          "description": "Prov Inc Taxi",
          "Units": 2400
        ,
        
          "description": "Cpp/Qpp",
          "Units": 2400
        ,
        
          "description": "Cpp/Qpp",
          "Units": 2400
        
      ]
    
  
];

    setState(() 
      for(Map js in jsonData)
        list.add(js);
      
    );
      

      @override
      Widget build(BuildContext context) 
        return Container(
            padding: EdgeInsets.all(20),
            color: Colors.white,
            child: list.length < 1
                ? ListTile(
                    leading: CircularProgressIndicator(), title: Text('Loading...'))
                : ListView.builder(
                    itemCount: list.length,
                    itemBuilder: (rCon, rInd) 
                      return Card(
                          color: Colors.blueAccent,
                          child: Column(
                            mainAxisSize: MainAxisSize.min,
                            crossAxisAlignment:CrossAxisAlignment.start,            
                                        children: [
                            Padding(
                              padding: const EdgeInsets.all(5.0),
                              child: Text('$list[rInd]['paystub_Details']['name']',
                                  style: TextStyle(
                                      color: Colors.white,
                                      fontWeight: FontWeight.bold,
                                      fontSize: 17)),
                            ),
                            Padding(
                              padding: const EdgeInsets.all(3.0),
                              child: Text(
                                  '$list[rInd]['paystub_Details']['position']',
                                  style: TextStyle(
                                      color: Colors.white,
                                      fontWeight: FontWeight.bold,
                                      fontSize: 15)),
                            ),
                            SizedBox(height: 10),
                            Container(
                              padding: const EdgeInsets.all(7.0),
                              height: 50,
                              child: ListView.builder(
                                  scrollDirection: Axis.horizontal,
                                  itemCount: list[rInd]['earn']['detail'].length,
                                  itemBuilder: (con, ind) 
                                    return Container(
                                        padding: const EdgeInsets.all(5.0),
                                        margin: const EdgeInsets.all(5.0),
                                        decoration: BoxDecoration(
                                            borderRadius: BorderRadius.circular(7),
                                            color: Colors.cyanAccent),
                                        child: Text(
                                            '$list[rInd]['earn']['detail'][ind]['Amount']',
                                            style: TextStyle(
                                                color: Colors.black,
                                                fontWeight: FontWeight.bold,
                                                fontSize: 15)));
                                  ),
                            )
                          ]));
                    ));
      

【讨论】:

用地图替换列表并使用直接卡片小部件而不是列表视图 是的,我做到了,但是我得到了一个错误未处理的异常:类型'List'不是类型'Map'的子类型 json 响应和图片中的完全一样吗? 您的 json 响应返回 json 数组!所以尝试映射 jsonData 并将每个项目添加到 list 谢谢你,我喜欢你的工作,你减轻了我的压力。我永远不会忘记这个帮助,爱你!!! @Naveen Avidi

以上是关于解码复杂的 JSON?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 dart/flutter 中解码复杂的 json?

如何为复杂对象编写自定义 JSON 解码器?

通过 PHP 将来自 MySQL 的 JSON 解码为 SwiftUI 中的复杂结构

用 Go 解析复杂 JSON 的思路

显示比“无法解码 JSON 对象”更好的错误消息

视频编解码 — 码控算法