首页 > 解决方案 > Flutter 将自定义 JSON 映射到对象

问题描述

我想从 WP REST API 获取图像 URL,我正在将我的帖子检索到列表中,所以获取 featuresImageUrl 是这样的:

一个)

//THIS GETS AN URL TO ATTACHMENT WHEN IM USING A LIST<DYNAMIC>
featuredMediaAtta = posts[index]["_embedded"]["wp:featuredmedia"][0]["source_url"],

结果是嵌入附件的 url,我想在B中得到相同的结果

B) 我不得不从列表更改为对象模型到列表>帖子模型是:

Post.fromMap(Map<String, dynamic> map) {
    if (map == null) {
      return;
    }

    ...

    //I can retrieve featured media id successfully 

featuresMediaID = map['featured_media'];

    // BUT HERE I CAN NOT RETURN THE ATTACHMENT URL
    featuredMediaAtta = map["_embedded"]["wp:featuredmedia"][0]["source_url"] ;
    commentStatus = map['comment_status'];

    ...

这是获取 http://www.ehawal.com/wp-json/wp/v2/posts/73335?_embed的 JSON 响应

{
    "id": 73335,
    "date": "2018-12-01T20:05:26",
    "date_gmt": "2018-12-01T20:05:26",
    "guid": {
        "rendered": "http://www.theUrl.com/?p=73335"
    },
    "modified": "2018-12-01T20:05:26",
    "modified_gmt": "2018-12-01T20:05:26",
    "slug": "%d8%a8%d9%88-%d9%88%db%8e%d9%86%d9%87%e2%80%8c%db%8c-%d9%86%d9%88%db%8e",
    "status": "publish",
    "type": "post",
    "link": "http://www.theUrl.com/2018/12/01/%d8%a8%d9%88-%d9%88%db%8e%d9%86%d9%87%e2%80%8c%db%8c-%d9%86%d9%88%db%8e/",
    "title": {
        "rendered": "بو وێنه\u200cی نوێ"
    },
    "content": {
        "rendered": "<p>وێمه\u200cی لكسادژڤ ؛لكژ اعس</p>\n<div class=\"theUrl_container\" 
        "protected": false
    },
    "excerpt": {
        "rendered": "<p>وێمه\u200cی لكسادژڤ ؛لكژ اعس</p>\n<div class=\"theUrl_container\" style=\"\"><!-- theUrl.com BEGIN --><span class=\"theUrl-wrapper\"  
    "author": 1,
    "featured_media": 73336,
    "comment_status": "open",
    "ping_status": "open",
    "sticky": false,
    "template": "",
    "format": "standard",
    "meta": [],
    "categories": [
        195,
        1,
        176
    ],
    "tags": [],
    "acf": [],
    "_links": {
        "self": [
            {
                "href": "http://www.theUrl.com/wp-json/wp/v2/posts/73335"
            }
        ],
        "collection": [
            {
                "href": "http://www.theUrl.com/wp-json/wp/v2/posts"
            }
        ],
        "about": [
            {
                "href": "http://www.theUrl.com/wp-json/wp/v2/types/post"
            }
        ],
        "author": [
            {
                "embeddable": true,
                "href": "http://www.theUrl.com/wp-json/wp/v2/users/1"
            }
        ],
        "replies": [
            {
                "embeddable": true,
                "href": "http://www.theUrl.com/wp-json/wp/v2/comments?post=73335"
            }
        ],
        "version-history": [
            {
                "count": 1,
                "href": "http://www.theUrl.com/wp-json/wp/v2/posts/73335/revisions"
            }
        ],
        "predecessor-version": [
            {
                "id": 73337,
                "href": "http://www.theUrl.com/wp-json/wp/v2/posts/73335/revisions/73337"
            }
        ],
        "wp:featuredmedia": [
            {
                "embeddable": true,
                "href": "http://www.theUrl.com/wp-json/wp/v2/media/73336"
            }
        ],
        "wp:attachment": [
            {
                "href": "http://www.theUrl.com/wp-json/wp/v2/media?parent=73335"
            }
        ],
        "wp:term": [
            {
                "taxonomy": "category",
                "embeddable": true,
                "href": "http://www.theUrl.com/wp-json/wp/v2/categories?post=73335"
            },
            {
                "taxonomy": "post_tag",
                "embeddable": true,
                "href": "http://www.theUrl.com/wp-json/wp/v2/tags?post=73335"
            }
        ],
        "curies": [
            {
                "name": "wp",
                "href": "https://api.w.org/{rel}",
                "templated": true
            }
        ]
    },
    "_embedded": {
        "author": [
            {
                "id": 1,
                "name": "hooshyar_nuc",
                "url": "",
                "description": "",
                "link": "http://www.theUrl.com/author/hooshyar_nuc/",
                "slug": "hooshyar_nuc",
                "avatar_urls": {
                    "24": "http://0.gravatar.com/avatar/f1a624f625d4271d52b7fc9445609eb5?s=24&d=mm&r=g",
                    "48": "http://0.gravatar.com/avatar/f1a624f625d4271d52b7fc9445609eb5?s=48&d=mm&r=g",
                    "96": "http://0.gravatar.com/avatar/f1a624f625d4271d52b7fc9445609eb5?s=96&d=mm&r=g"
                },
                "acf": [],
                "_links": {
                    "self": [
                        {
                            "href": "http://www.theUrl.com/wp-json/wp/v2/users/1"
                        }
                    ],
                    "collection": [
                        {
                            "href": "http://www.theUrl.com/wp-json/wp/v2/users"
                        }
                    ]
                }
            }
        ],
        "wp:featuredmedia": [
            {
                "id": 73336,
                "date": "2018-12-01T20:05:10",
                "slug": "guardian",
                "type": "attachment",
                "link": "http://www.theUrl.com/2018/12/01/%d8%a8%d9%88-%d9%88%db%8e%d9%86%d9%87%e2%80%8c%db%8c-%d9%86%d9%88%db%8e/guardian/",
                "title": {
                    "rendered": "guardian"
                },
                "author": 1,
                "acf": [],
                "caption": {
                    "rendered": ""
                },
                "alt_text": "",
                "media_type": "image",
                "mime_type": "image/png",
                "media_details": {
                    "width": 400,
                    "height": 155,
                    "file": "2018/12/guardian.png",
                    "sizes": {
                        "thumbnail": {
                            "file": "guardian-150x150.png",
                            "width": 150,
                            "height": 150,
                            "mime_type": "image/png",
                            "source_url": "http://www.theUrl.com/wp-content/uploads/2018/12/guardian-150x150.png"
                        },
                        "medium": {
                            "file": "guardian-300x116.png",
                            "width": 300,
                            "height": 116,
                            "mime_type": "image/png",
                            "source_url": "http://www.theUrl.com/wp-content/uploads/2018/12/guardian-300x116.png"
                        },
                        "tie-small": {
                            "file": "guardian-110x75.png",
                            "width": 110,
                            "height": 75,
                            "mime_type": "image/png",
                            "source_url": "http://www.theUrl.com/wp-content/uploads/2018/12/guardian-110x75.png"
                        },
                        "tie-medium": {
                            "file": "guardian-310x155.png",
                            "width": 310,
                            "height": 155,
                            "mime_type": "image/png",
                            "source_url": "http://www.theUrl.com/wp-content/uploads/2018/12/guardian-310x155.png"
                        },
                        "tie-large": {
                            "file": "guardian-310x155.png",
                            "width": 310,
                            "height": 155,
                            "mime_type": "image/png",
                            "source_url": "http://www.theUrl.com/wp-content/uploads/2018/12/guardian-310x155.png"
                        },
                        "full": {
                            "file": "guardian.png",
                            "width": 400,
                            "height": 155,
                            "mime_type": "image/png",
                            "source_url": "http://www.theUrl.com/wp-content/uploads/2018/12/guardian.png"
                        }
                    },
                    "image_meta": {
                        "aperture": "0",
                        "credit": "",
                        "camera": "",
                        "caption": "",
                        "created_timestamp": "0",
                        "copyright": "",
                        "focal_length": "0",
                        "iso": "0",
                        "shutter_speed": "0",
                        "title": "",
                        "orientation": "0",
                        "keywords": []
                    }
                },
                "source_url": "http://www.theUrl.com/wp-content/uploads/2018/12/guardian.png",
                "_links": {
                    "self": [
                        {
                            "attributes": [],
                            "href": "http://www.theUrl.com/wp-json/wp/v2/media/73336"
                        }
                    ],
                    "collection": [
                        {
                            "attributes": [],
                            "href": "http://www.theUrl.com/wp-json/wp/v2/media"
                        }
                    ],
                    "about": [
                        {
                            "attributes": [],
                            "href": "http://www.theUrl.com/wp-json/wp/v2/types/attachment"
                        }
                    ],
                    "author": [
                        {
                            "attributes": {
                                "embeddable": true
                            },
                            "href": "http://www.theUrl.com/wp-json/wp/v2/users/1"
                        }
                    ],
                    "replies": [
                        {
                            "attributes": {
                                "embeddable": true
                            },
                            "href": "http://www.theUrl.com/wp-json/wp/v2/comments?post=73336"
                        }
                    ]
                }
            }
        ],
        "wp:term": [
            [
                {
                    "id": 195,
                    "link": "http://www.theUrl.com/category/world/",
                    "name": "جیهان",
                    "slug": "world",
                    "taxonomy": "category",
                    "acf": [],
                    "_links": {
                        "self": [
                            {
                                "href": "http://www.theUrl.com/wp-json/wp/v2/categories/195"
                            }
                        ],
                        "collection": [
                            {
                                "href": "http://www.theUrl.com/wp-json/wp/v2/categories"
                            }
                        ],
                        "about": [
                            {
                                "href": "http://www.theUrl.com/wp-json/wp/v2/taxonomies/category"
                            }
                        ],
                        "wp:post_type": [
                            {
                                "href": "http://www.theUrl.com/wp-json/wp/v2/posts?categories=195"
                            }
                        ],
                        "curies": [
                            {
                                "name": "wp",
                                "href": "https://api.w.org/{rel}",
                                "templated": true
                            }
                        ]
                    }
                },
                {
                    "id": 1,
                    "link": "http://www.theUrl.com/category/all/",
                    "name": "سه\u200cره\u200cكی",
                    "slug": "all",
                    "taxonomy": "category",
                    "acf": [],
                    "_links": {
                        "self": [
                            {
                                "href": "http://www.theUrl.com/wp-json/wp/v2/categories/1"
                            }
                        ],
                        "collection": [
                            {
                                "href": "http://www.theUrl.com/wp-json/wp/v2/categories"
                            }
                        ],
                        "about": [
                            {
                                "href": "http://www.theUrl.com/wp-json/wp/v2/taxonomies/category"
                            }
                        ],
                        "wp:post_type": [
                            {
                                "href": "http://www.theUrl.com/wp-json/wp/v2/posts?categories=1"
                            }
                        ],
                        "curies": [
                            {
                                "name": "wp",
                                "href": "https://api.w.org/{rel}",
                                "templated": true
                            }
                        ]
                    }
                },
                {
                    "id": 176,
                    "link": "http://www.theUrl.com/category/grng/",
                    "name": "گرنگ",
                    "slug": "grng",
                    "taxonomy": "category",
                    "acf": [],
                    "_links": {
                        "self": [
                            {
                                "href": "http://www.theUrl.com/wp-json/wp/v2/categories/176"
                            }
                        ],
                        "collection": [
                            {
                                "href": "http://www.theUrl.com/wp-json/wp/v2/categories"
                            }
                        ],
                        "about": [
                            {
                                "href": "http://www.theUrl.com/wp-json/wp/v2/taxonomies/category"
                            }
                        ],
                        "wp:post_type": [
                            {
                                "href": "http://www.theUrl.com/wp-json/wp/v2/posts?categories=176"
                            }
                        ],
                        "curies": [
                            {
                                "name": "wp",
                                "href": "https://api.w.org/{rel}",
                                "templated": true
                            }
                        ]
                    }
                }
            ],
            []
        ]
    }
}

标签: jsonwordpressdartflutterwordpress-rest-api

解决方案


推荐阅读