解析Google自定义搜索API Swift

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解析Google自定义搜索API Swift相关的知识,希望对你有一定的参考价值。

我正在尝试从下面的Google自定义搜索API中解析“元”数组的字段“ og:image”和“ og:title”。老实说,我不知道如何做到这一点,因为我无法在线找到任何资源。我需要检索此信息,因为我需要在我的应用程序中显示字段的内容。如果无法仅解析“ metatags”数组,如何对整个API进行解析?谢谢

    {
  "kind": "customsearch#search",
  "url": {
    "type": "application/json",
    "template": "https://www.googleapis.com/customsearch/v1?q={searchTerms}&num={count?}&start={startIndex?}&lr={language?}&safe={safe?}&cx={cx?}&sort={sort?}&filter={filter?}&gl={gl?}&cr={cr?}&googlehost={googleHost?}&c2coff={disableCnTwTranslation?}&hq={hq?}&hl={hl?}&siteSearch={siteSearch?}&siteSearchFilter={siteSearchFilter?}&exactTerms={exactTerms?}&excludeTerms={excludeTerms?}&linkSite={linkSite?}&orTerms={orTerms?}&relatedSite={relatedSite?}&dateRestrict={dateRestrict?}&lowRange={lowRange?}&highRange={highRange?}&searchType={searchType}&fileType={fileType?}&rights={rights?}&imgSize={imgSize?}&imgType={imgType?}&imgColorType={imgColorType?}&imgDominantColor={imgDominantColor?}&alt=json"
  },
  "queries": {
    "request": [
      {
        "title": "Google Custom Search - 4009900360937",
        "totalResults": "6",
        "searchTerms": "4009900360937",
        "count": 6,
        "startIndex": 1,
        "inputEncoding": "utf8",
        "outputEncoding": "utf8",
        "safe": "off",
        "cx": ""
      }
    ]
  },
  "context": {
    "title": "Google"
  },
  "searchInformation": {
    "searchTime": 0.36736,
    "formattedSearchTime": "0.37",
    "totalResults": "6",
    "formattedTotalResults": "6"
  },
  "items": [
    {
      "kind": "customsearch#result",
      "title": "Wrigleys Airwaves Menthol & Eukalyptus 3x 10 Dragees ...",
      "htmlTitle": "Wrigleys Airwaves Menthol & Eukalyptus 3x 10 Dragees ...",
      "link": "https://www.codecheck.info/m/essen/suesswaren/kaugummi_kaudragees/ean_4009900360937/id_368832/Wrigleys_Airwaves_Menthol_Eukalyptus_3x_10_Dragees.pro",
      "displayLink": "www.codecheck.info",
      "snippet": "Wrigleys Airwaves Menthol & Eukalyptus 3x 10 Dragees. Kaugummi & 
Kaudragees. Produktbeschreibung. Menthol kaugummis. Menge / Grösse. 3 
Stangen a ...",
      "htmlSnippet": "Wrigleys Airwaves Menthol & Eukalyptus 3x 10 Dragees. Kaugummi & u003cbru003e
Kaudragees. Produktbeschreibung. Menthol kaugummis. Menge / Grösse. 3 u003cbru003e
Stangen a ...",
      "cacheId": "f04ddBPMjcMJ",
      "formattedUrl": "https://www.codecheck.info/...4009900360937/.../Wrigleys_Airwaves_ Menthol_Eukalyptus_3x_10_Dragees.pro",
      "htmlFormattedUrl": "https://www.codecheck.info/...u003cbu003e4009900360937u003c/bu003e/.../Wrigleys_Airwaves_ Menthol_Eukalyptus_3x_10_Dragees.pro",
      "pagemap": {
        "cse_thumbnail": [
          {
            "src": "https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQuvPSLNQtg0Ie2_MxSlKIIRNWENp1ezJboQcYIpatdxd5QDks4eJnRIrY",
            "width": "225",
            "height": "225"
          }
        ],
        "metatags": [
          {
            "apple-itunes-app": "app-id=359351047",
            "og:image": "http://www.codecheck.info/img/51821803/1",
            "og:type": "Product",
            "twitter:card": "summary",
            "twitter:title": "Wrigleys Airwaves Menthol & Eukalyptus 3x 10 Dragees",
            "og:site_name": "Codecheck.info",
            "twitter:domain": "CodeCheck",
            "twitter:url": "http://www.codecheck.info/m/essen/suesswaren/kaugummi_kaudragees/ean_4009900360937/id_368832/Wrigleys_Airwaves_Menthol_Eukalyptus_3x_10_Dragees.pro",
            "og:title": "Wrigleys Airwaves Menthol & Eukalyptus 3x 10 Dragees",
            "language": "DE",
            "og:description": "Wrigleys Airwaves Menthol & Eukalyptus 3x 10 Dragees",
            "twitter:image": "http://www.codecheck.info/img/51821803/1",
            "fb:app_id": "122709251073903",
            "apple-mobile-web-app-status-bar-style": "black",
            "viewport": "width=device-width, initial-scale=1, minimal-ui, user-scalable=0, maximum-scale=1",
            "apple-mobile-web-app-capable": "yes",
            "twitter:description": "Wrigleys Airwaves Menthol & Eukalyptus 3x 10 Dragees",
            "og:locale": "de_DE",
            "publisher": "Codecheck.info",
            "og:url": "http://www.codecheck.info/m/essen/suesswaren/kaugummi_kaudragees/ean_4009900360937/id_368832/Wrigleys_Airwaves_Menthol_Eukalyptus_3x_10_Dragees.pro",
            "google-play-app": "app-id=ch.ethz.im.codecheck",
            "format-detection": "telephone=no"
          }
        ],
        "cse_image": [
          {
            "src": "http://www.codecheck.info/img/51821803/1"
          }
        ]
      }
    },
    {
      "kind": "customsearch#result",
      "title": "NEW WRIGLEY'S AIRWAVES SUGAR FREE CHEWING GUM 3 PACK",
      "htmlTitle": "NEW WRIGLEY'S AIRWAVES SUGAR FREE CHEWING GUM 3 PACK",
      "link": "https://www.ebay.co.uk/itm/NEW-WRIGLEYS-AIRWAVES-SUGAR-FREE-CHEWING-GUM-3-PACK-30-PIECES-42g-/292246791829",
      "displayLink": "www.ebay.co.uk",
      "snippet": "... Gummy, Soft Sweet & Marshmallow. Food Aisle: Pantry, Food Specifications: 
Sugar Free. Brand: Wrigley, Type: Chewing/ Bubble Gum. EAN: 4009900360937
 ...",
      "htmlSnippet": "... Gummy, Soft Sweet & Marshmallow. Food Aisle: Pantry, Food Specifications: u003cbru003e
Sugar Free. Brand: Wrigley, Type: Chewing/ Bubble Gum. EAN: u003cbu003e4009900360937u003c/bu003eu003cbru003e
 ...",
      "cacheId": "fOoNmVeHk30J",
      "formattedUrl": "https://www.ebay.co.uk/itm/NEW-WRIGLEYS...3...-/292246791829",
      "htmlFormattedUrl": "https://www.ebay.co.uk/itm/NEW-WRIGLEYS...3...-/292246791829",
      "pagemap": {
        "offer": [
          {
            "areaserved": "United Kingdom and many other countries | See details",
            "availableatorfrom": "Luton, United Kingdom",
            "pricecurrency": "GBP",
            "price": "3.69",
            "availability": "https://schema.org/InStock",
            "itemcondition": "New"
          }
        ],
        "cse_thumbnail": [
          {
            "src": "https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcTcul7IOdYe6L0tYz-KZzyMuqXmIGrCxSdEwjXLdDWiT-GVfeXZe2X9moE",
            "width": "248",
            "height": "204"
          }
        ],
        "product": [
          {
            "image": "https://i.ebayimg.com/images/g/BDkAAOSwA1pawWWP/s-l300.jpg",
            "name": "Details about NEW WRIGLEY'S AIRWAVES SUGAR FREE CHEWING GUM 3 PACK - 30 PIECES - 42g"
          }
        ],
        "review": [
          {
            "reviewdate": "2019-10-30",
            "reviewer": "peter55_1"
          },
          {
            "author": "peter55_1",
            "name": "Nice one",
            "reviewbody": "Hi there..Nice one..ok cool..many thanks..Peter",
            "datepublished": "30 Oct, 2019"
          },
          {
            "author": "spike.7900",
            "name": "Nice one !!!",
            "reviewbody": "Yes great !!!",
            "datepublished": "24 May, 2018"
          }
        ],
        "aggregaterating": [
          {
            "ratingvalue": "5.0",
            "reviewcount": "2"
          }
        ],
        "metatags": [
          {
            "og:image": "https://i.ebayimg.com/images/g/BDkAAOSwA1pawWWP/s-l400.jpg",
            "twitter:card": "summary",
            "twitter:title": "NEW WRIGLEY'S AIRWAVES SUGAR FREE CHEWING GUM 3 PACK - 30 PIECES - 42g",
            "og:type": "ebay-objects:item",
            "og:site_name": "eBay",
            "msvalidate.01": "34E98E6F27109BE1A9DCF19658EEEE33",
            "og:title": "NEW WRIGLEY'S AIRWAVES SUGAR FREE CHEWING GUM 3 PACK - 30 PIECES - 42g 4009900360937 | eBay",
            "y_key": "0090e7e28a2554d4",
            "og:description": "Find many great new & used options and get the best deals for NEW WRIGLEY'S AIRWAVES SUGAR FREE CHEWING GUM 3 PACK - 30 PIECES - 42g at the best online prices at eBay! Free delivery for many products!",
            "twitter:image": "https://i.ebayimg.com/images/g/BDkAAOSwA1pawWWP/s-l400.jpg",
            "layout": "main",
            "referrer": "unsafe-url",
            "fb:app_id": "102628213125203",
            "twitter:site": "@eBay",
            "twitter:description": "Find many great new & used options and get the best deals for NEW WRIGLEY'S AIRWAVES SUGAR FREE CHEWING GUM 3 PACK - 30 PIECES - 42g at the best online prices at eBay! Free delivery for many products!",
            "og:url": "https://i.ebayimg.com/images/g/BDkAAOSwA1pawWWP/s-l400.jpg"
          }
        ],
        "cse_image": [
          {
            "src": "https://i.ebayimg.com/images/g/BDkAAOSwA1pawWWP/s-l400.jpg"
          }
        ],
        "hproduct": [
          {
            "fn": "Details about NEW WRIGLEY'S AIRWAVES SUGAR FREE CHEWING GUM 3 PACK - 30 PIECES - 42g",
            "photo": "https://i.ebayimg.com/images/g/BDkAAOSwA1pawWWP/s-l300.jpg",
            "currency": "GBP",
            "currency_iso4217": "826"
          }
        ],
        "listitem": [
          {
            "item": "Home, Furniture & DIY",
            "name": "Home, Furniture & DIY",
            "position": "1"
          },
          {
            "item": "Food & Drink",
            "name": "Food & Drink",
            "position": "2"
          },
          {
            "item": "Food Cupboard",
            "name": "Food Cupboard",
            "position": "3"
          },
          {
            "item": "Sweets & Chocolate",
            "name": "Sweets & Chocolate",
            "position": "4"
          },
          {
            "item": "Candies",
            "name": "Candies",
            "position": "5"
          },
          {
            "item": "Gummy, Soft Sweets & Marshmallows",
            "name": "Gummy, Soft Sweets & Marshmallows",
            "position": "6"
          },
          {
            "item": "See more Wrigley S Airwaves Menthol Eucalyptus Sugarfre...",
            "name": "See more Wrigley S Airwaves Menthol Eucalyptus Sugarfre...",
            "position": "1"
          }
        ]
      }
    },
    {
      "kind": "customsearch#result",
      "title": "Airwaves Menthol & Eucalyptus 3x10er | Online kaufen im World of ...",
      "htmlTitle": "Airwaves Menthol & Eucalyptus 3x10er | Online kaufen im World of ...",
      "link": "https://www.worldofsweets.de/Airwaves-Menthol-Eucalyptus-3x10er.311211.html",
      "displayLink": "www.worldofsweets.de",
      "snippet": "Multipack mit 3x10 zuckerfreien Kaugummi-Dragees mit Menthol- und 
Eukalyptusgeschmack. Inhalt: 45g. Herkunftsland: USA. EAN: 4009900360937. 
Art.-Nr.: ...",
      "htmlSnippet": "Multipack mit 3x10 zuckerfreien Kaugummi-Dragees mit Menthol- und u003cbru003e
Eukalyptusgeschmack. Inhalt: 45g. Herkunftsland: USA. EAN: u003cbu003e4009900360937u003c/bu003e. u003cbru003e
Art.-Nr.: ...",
      "cacheId": "5xWyO_gVRoAJ",
      "formattedUrl": "https://www.worldofsweets.de/Airwaves-Menthol-Eucalyptus-3x10er.311211 .html",
      "htmlFormattedUrl": "https://www.worldofsweets.de/Airwaves-Menthol-Eucalyptus-3x10er.311211 .html",
      "pagemap": {
        "cse_thumbnail": [
          {
            "src": "https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSBeGT1ypG_5KuTTlilOOrkqyjzIIVK_1ibx00UcNfp0-QML81xmqk-QfHF",
            "width": "225",
            "height": "225"
          }
        ],
        "metatags": [
          {
            "msapplication-tilecolor": "#D83434",
            "og:image": "https://www.worldofsweets.de/out/pictures/generated/product/1/540_340_75/airwaves-menthol--amp--eucalyptus-3er.jpg",
            "og:type": "product",
            "og:site_name": "https://www.worldofsweets.de/",
            "viewport": "width=device-width, initial-scale=1, maximum-scale=2, minimum-scale=1",
            "og:title": "Airwaves Menthol & Eucalyptus 3x10er | Online kaufen im World of Sweets Shop",
            "og:url": "https://www.worldofsweets.de/Airwaves-Menthol-Eucalyptus-3x10er.311211.html",
            "msapplication-tileimage": "https://www.worldofsweets.de/out/worldofsweets/img/favicons/favicon_512x512.png",
            "og:description": "Airwaves Menthol & Eucalyptus 3x10er - Airwaves Menthol & Eukalyptus verbindet, was zusammen gehört. Die Kaugummi-Dragees kombinieren einen erfrischenden Eukalyptus-Geschmack mit dem intensiven Airwav"
          }
        ],
        "cse_image": [
          {
            "src": "https://www.worldofsweets.de/out/pictures/generated/product/1/540_340_75/airwaves-menthol--amp--eucalyptus-3er.jpg"
          }
        ]
      }
    },
    {
      "kind": "customsearch#result",
      "title": "Swift JSON parsing array of dictionaries - Stack Overflow",
      "htmlTitle": "Swift JSON parsing array of dictionaries - Stack Overflow",
      "link": "https://stackoverflow.com/questions/60338583/swift-json-parsing-array-of-dictionaries",
      "displayLink": "stackoverflow.com",
      "snippet": "6 days ago ... Reason: products in the above JSON is not of type [[String: String]] . It is of type [[
String: Any]] instead. That's the reason parsing is giving error.",
      "htmlSnippet": "6 days ago u003cbu003e...u003c/bu003e Reason: products in the above JSON is not of type [[String: String]] . It is of type [[u003cbru003e
String: Any]] instead. That's the reason parsing is giving error.",
      "cacheId": "Ipcat4pAuKIJ",
      "formattedUrl": "https://stackoverflow.com/questions/.../swift-json-parsing-array-of- dictionaries",
      "htmlFormattedUrl": "https://stackoverflow.com/questions/.../swift-json-parsing-array-of- dictionaries",
      "pagemap": {
        "question": [
          {
            "image": "https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon.png?v=c78bd457575a",
            "upvotecount": "-2",
            "answercount": "2",
            "name": "Swift JSON parsing array of dictionaries",
            "text": "I am trying to parse this JSON file which returns an array of dictionaries. I mainly need to retrieve the 'barcode_number' and 'images' values of the dictionaries in order to use them in my...",
            "datecreated": "2020-02-21T12:22:11",
            "url": "share"
          }
        ],
        "answer": [
          {
            "upvotecount": "1",
            "text": "Reason: products in the above JSON is not of type [[String: String]]. It is of type [[String: Any]] instead. That's the reason parsing is giving error. Solution: You need to create a separate...",
            "datecreated": "2020-02-21T12:37:17",
            "url": "https://stackoverflow.com/questions/60338583/swift-json-parsing-array-of-dictionaries/60338812#60338812"
          },
          {
            "upvotecount": "0",
            "text": "try this: yes, it looks like a lot of work, but it wasn't. How did i do this? Easy: 1) enter your url in a webbrowser and copy the result 2) go to https://app.quicktype.io/ and enter the copy...",
            "datecreated": "2020-02-21T12:42:47",
            "url": "https://stackoverflow.com/questions/60338583/swift-json-parsing-array-of-dictionaries/60338906#60338906"
          }
        ],
        "person": [
          {
            "name": "Luca S"
          },
          {
            "name": "PGDev"
          },
          {
            "name": "Chris"
          }
        ],
        "metatags": [
          {
            "viewport": "width=device-width, height=device-height, initial-scale=1.0, minimum-scale=1.0"
          }
        ]
      }
    },
    {
      "kind": "customsearch#result",
      "title": "Airwaves Menthol & Eucalyptus 3x10er Multipack",
      "htmlTitle": "Airwaves Menthol & Eucalyptus 3x10er Multipack",
      "link": "https://fr.openfoodfacts.org/produit/4009900360937/airwaves-menthol-eucalyptus-3x10er-multipack",
      "displayLink": "fr.openfoodfacts.org",
      "snippet": "10 avr. 2018 ... Airwaves Menthol & Eucalyptus 3x10er Multipack. Code barre : 4009900360937 
(EAN / EAN-13). La page de ce produit n'est pas complète.",
      "htmlSnippet": "10 avr. 2018 u003cbu003e...u003c/bu003e Airwaves Menthol & Eucalyptus 3x10er Multipack. Code barre : u003cbu003e4009900360937u003c/bu003e u003cbru003e
(EAN / EAN-13). La page de ce produit n'est pas complète.",
      "cacheId": "n1RLGxrSrnQJ",
      "formattedUrl": "https://fr.openfoodfacts.org/.../4009900360937/airwaves-menthol-eucalyptus -3x10er-multipack",
      "htmlFormattedUrl": "https://fr.openfoodfacts.org/.../u003cbu003e4009900360937u003c/bu003e/airwaves-menthol-eucalyptus -3x10er-multipack",
      "pagemap": {
        "cse_thumbnail": [
          {
            "src": "https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcRIicojCdKeyBJkuzIZgC1qkkWHynDQQs-yjMOnGASpIczNComl2ya4aJQH",
            "width": "252",
            "height": "200"
          }
        ],
        "product": [
          {
            "gtin13": "4009900360937",
            "name": "Airwaves Menthol & Eucalyptus 3x10er Multipack",
            "brand": "Airwaves"
          }
        ],
        "imageobject": [
          {
            "license": "https://creativecommons.org/licenses/by-sa/3.0/",
            "thumbnail": "https://static.openfoodfacts.org/images/products/400/990/036/0937/ingredients_fr.4.200.jpg",
            "contenturl": "https://static.openfoodfacts.org/images/products/400/990/036/0937/ingredients_fr.4.full.jpg",
            "representativeofpage": "false",
            "imgid": "ingredients_fr",
            "caption": "Airwaves Menthol & Eucalyptus 3x10er Multipack - Ingrédients"
          },
          {
            "license": "https://creativecommons.org/licenses/by-sa/3.0/",
            "thumbnail": "https://static.openfoodfacts.org/images/products/400/990/036/0937/ingredients_fr.4.200.jpg",
            "contenturl": "https://static.openfoodfacts.org/images/products/400/990/036/0937/ingredients_fr.4.full.jpg",
            "representativeofpage": "false",
            "imgid": "ingredients_fr",
            "caption": "Airwaves Menthol & Eucalyptus 3x10er Multipack - Ingrédients"
          }
        ],
        "metatags": [
          {
            "msapplication-tilecolor": "#da532c",
            "og:image": "https://static.openfoodfacts.org/images/misc/openfoodfacts-logo-fr-356.png",
            "msapplication-config": "https://static.openfoodfacts.org/images/favicon/browserconfig.xml",
            "apple-itunes-app": "app-id=588797948",
            "twitter:card": "product",
            "twitter:title": "Airwaves Menthol & Eucalyptus 3x10er Multipack",
            "og:type": "food",
            "twitter:label1": "marque",
            "twitter:label2": "catégorie",
            "msapplication-tileimage": "https://static.openfoodfacts.org/images/favicon/mstile-144x144.png",
            "twitter:creator": "@OpenFoodFactsFr",
            "og:description": "Ingrédients, composition nutritionnelle et information sur les produits alimentaires du monde entier dans une base de données libre et ouverte",
            "twitter:data1": "Airwaves",
            "_globalsign-domain-verification": "2ku73dDL0bAPTj_s1aylm6vxvrBZFK59SfbH_RdUya",
            "fb:app_id": "219331381518041",
            "twitter:site": "@OpenFoodFactsFr",
            "viewport": "width=device-width, initial-scale=1.0",
            "twitter:description": "Ingrédients, allergènes, additifs, composition nutritionnelle, labels, origine des ingrédients et informations du produit Airwaves Menthol & Eucalyptus 3x10er Multipack",
            "flattr:id": "dw637l",
            "og:url": "https://fr.openfoodfacts.org/produit/4009900360937/airwaves-menthol-eucalyptus-3x10er-multipack"
          }
        ],
        "cse_image": [
          {
            "src": "https://static.openfoodfacts.org/images/misc/openfoodfacts-logo-fr-356.png"
          }
        ],
        "hproduct": [
          {
            "fn": "Airwaves Menthol & Eucalyptus 3x10er Multipack"
          }
        ]
      }
    },
    {
      "kind": "customsearch#result",
      "title": "Airwaves Menthol & Eukalyptus (3 St.) Kaugummi | real",
      "htmlTitle": "Airwaves Menthol & Eukalyptus (3 St.) Kaugummi | real",
      "link": "https://www.real.de/product/320670591/",
      "displayLink": "www.real.de",
      "snippet": "4009900360937. Herstellernummer. 133648005. Hersteller. Wrigley, Wrigley 
Kaugummis, Wrigley Süßigkeiten & Gebäck. Gewicht. 50 g. Zutatenverzeichnis.",
      "htmlSnippet": "u003cbu003e4009900360937u003c/bu003e. Herstellernummer. 133648005. Hersteller. Wrigley, Wrigley u003cbru003e
Kaugummis, Wrigley Süßigkeiten & Gebäck. Gewicht. 50 g. Zutatenverzeichnis.",
      "cacheId": "EAaYPSRT6tAJ",
      "formattedUrl": "https://www.real.de/product/320670591/",
      "htmlFormattedUrl": "https://www.real.de/product/320670591/",
      "pagemap": {
        "offer": [
          {
            "pricecurrency": "EUR",
            "price": "2.56",
            "availability": "http://schema.org/InStock",
            "itemcondition": "http://schema.org/NewCondition"
          }
        ],
        "cse_thumbnail": [
          {
            "src": "https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcR_yzLY6rSwIKrg5flkC2Zarb1TnLcEejdr-yRO_ShbUK4WQz7Sp78C3-o",
            "width": "225",
            "height": "224"
          }
        ],
        "product": [
          {
            "image": "https://media.real-onlineshop.de/images/items/1024x1024/dd6e657038ea57ce35ef738ae7c19a1e.jpg",
            "gtin13": "4009900360937",
            "name": "Airwaves Menthol & Eukalyptus (3 St.)",
            "description": "Produktbeschreibung Das original starke Geschmackserlebnis für den einzigartigen Airwaves Frische-Kick. Zuckerfreier Kaugummi mit Süßungsmitteln und Menthol- und Eukalyptusgeschmack. Zuckerfreih...",
            "sku": "320670591",
            "brand": "Wrigley"
          }
        ],
        "metatags": [
          {
            "og:image": "https://media.real-onlineshop.de/images/items/1024x1024/dd6e657038ea57ce35ef738ae7c19a1e.jpg",
            "theme-color": "#003A70",
            "og:type": "product",
            "og:site_name": "real.de",
            "og:title": "Airwaves Menthol & Eukalyptus (3 St.)",
            "product:price:amount": "2.56",
            "product:availability": "in stock",
            "og:description": "Das original starke Geschmackserlebnis für den einzigartigen Airwaves Frische-Kick. Zuckerfreier Kaugummi mit Süßungsmitteln und Menthol- und Eukalyptusgeschmack.",
            "fb:pages": "101656796570383",
            "fb:app_id": "198334153630763",
            "viewport": "width=device-width, initial-scale=1",
            "product:brand": "Wrigley",
            "product:price:currency": "EUR",
            "og:url": "https://www.real.de/product/320670591/"
          }
        ],
        "cse_image": [
          {
            "src": "https://media.real-onlineshop.de/images/items/1024x1024/dd6e657038ea57ce35ef738ae7c19a1e.jpg"
          }
        ],
        "hproduct": [
          {
            "fn": "Airwaves Menthol & Eukalyptus (3 St.)",
            "description": "Produktbeschreibung Das original starke Geschmackserlebnis für den einzigartigen Airwaves Frische-Kick. Zuckerfreier Kaugummi mit Süßungsmitteln und Menthol- und Eukalyptusgeschmack. Zuckerfreih...",
            "photo": "https://media.real-onlineshop.de/images/items/1024x1024/dd6e657038ea57ce35ef738ae7c19a1e.jpg",
            "currency": "EUR",
            "currency_iso4217": "978"
          }
        ],
        "listitem": [
          {
            "item": "https://www.real.de/",
            "name": "Startseite",
            "position": "1"
          },
          {
            "item": "Lebensmittel",
            "name": "Lebensmittel",
            "position": "2"
          },
          {
            "item": "Süßigkeiten & Gebäck",
            "name": "Süßigkeiten & Gebäck",
            "position": "3"
          },
          {
            "item": "Kaugummis",
            "name": "Kaugummis",
            "position": "4"
          },
          {
            "name": "Airwaves Menthol & Eukalyptus (3 St.)",
            "position": "5"
          }
        ]
      }
    }
  ]
}
答案

使用Codable解析JSON data

下面是模型,仅将metaTags解析为items

struct Root: Codable {
    let items: [Item]
}

struct Item: Codable {
    let pagemap: Pagemap
}

struct Pagemap: Codable {
    let metatags: [Metatag]
}

struct Metatag: Codable {
    let ogImage: String?
    let ogTitle: String?

    enum CodingKeys: String, CodingKey {
        case ogImage = "og:image"
        case ogTitle = "og:title"
    }
}

Parsedatalike

do {
    let decoder = JSONDecoder()
    decoder.keyDecodingStrategy = .convertFromSnakeCase
    let response = try decoder.decode(Root.self, from: data)
    print(response.items.map({ $0.pagemap.metatags.map({ $0.ogImage }) }))
} catch {
    print(error)
}

以上是关于解析Google自定义搜索API Swift的主要内容,如果未能解决你的问题,请参考以下文章

自定义搜索引擎

Google 自定义搜索 API - 反向图像搜索

如何自定义附近地点搜索返回的结果,Google Maps JS API V3

AWS Lambda:OpenBLAS 警告 - 无法确定此系统上的 L2 缓存大小,假设为 256k - 使用 Google 自定义搜索 API 时

如何将 Google 地图搜索框添加到我的自定义地图?

谷歌自定义搜索 JSON API