Flutter-关于Json转Dart要点

174 阅读2分钟

打个比方,接口获取到的数据如下:

{

    "code": 0,

    "msg": "成功",

    "data": {

        "items": [

            {

                "id": "640266",

                "title": "外交部驻港公署驳卡梅伦:殖民旧梦可以休矣",

                "pic": "https://i.guancha.cn/news/2024/03/20/20240320074402499.jpg",

                "summary": "",

                "show_type": 13,

                "special": null,

                "news_time": "2小时前",

                "news_type": 1,

                "comment_num": 86,

                "author": null,

                "user": {

                    "id": "",

                    "nick": "齐倩",

                    "pic": "",

                    "level_icon": ""

                },

                "url": "",

                "view_count": 93748,

                "view_count_text": "93748",

                "hot": false,

                "hot_comment": "为空"

            },

            {

                "id": "640266",

                "title": "外交部驻港公署驳卡梅伦:殖民旧梦可以休矣",

                "pic": "https://i.guancha.cn/news/2024/03/20/20240320074402499.jpg",

                "summary": "",

                "show_type": 13,

                "special": {

                    "id": "XiangGang",

                    "name": "香港"

                },

                "news_time": "2小时前",

                "news_type": 1,

                "comment_num": 86,

                "author": {

                    "id": "andelie",

                    "name": "安德烈·科尔图诺夫",

                    "pic": "https://i.guancha.cn/authors/andelie/20220524105234640.png",

                    "description": "俄罗斯国际事务委员会学术委员会主任",

                    "is_more": false

                },

                "user": {

                    "id": "",

                    "nick": "齐倩",

                    "pic": "",

                    "level_icon": ""

                },

                "url": "",

                "view_count": 93748,

                "view_count_text": "93748",

                "hot": false,

                "hot_comment": "为空"

            }

        ]

    }

}

不难发现,个别参数比如special本来正常返回是字典,但是有时候返回却是null,这样在使用JsonToDart进行转换的时候就容易报错,如果嵌套很深的话会更难改

image.png

所以个人建议,类似字典数组这种格式,找一个字典最齐全的那个进行补全,然后再去转换,这样就不会报错,修改如下:

{

    "code": 0,

    "msg": "成功",

    "data": {

        "items": [

            {

                "id": "640266",

                "title": "外交部驻港公署驳卡梅伦:殖民旧梦可以休矣",

                "pic": "https://i.guancha.cn/news/2024/03/20/20240320074402499.jpg",

                "summary": "",

                "show_type": 13,

                "special": {

                    "id": "XiangGang",

                    "name": "香港"

                },

                "news_time": "2小时前",

                "news_type": 1,

                "comment_num": 86,

                "author": {

                    "id": "andelie",

                    "name": "安德烈·科尔图诺夫",

                    "pic": "https://i.guancha.cn/authors/andelie/20220524105234640.png",

                    "description": "俄罗斯国际事务委员会学术委员会主任",

                    "is_more": false

                },

                "user": {

                    "id": "",

                    "nick": "齐倩",

                    "pic": "",

                    "level_icon": ""

                },

                "url": "",

                "view_count": 93748,

                "view_count_text": "93748",

                "hot": false,

                "hot_comment": "为空"

            }

        ]

    }

}

JsonToDart:www.geekailab.com/io/tools/js…

Json格式化:www.json.cn/

Json在线编辑:www.bejson.com/jsoneditoro…