嘿,大家好,我有这个https://www.googleapis.com/customsearch/v1?key=********&cx=017576662512468239146:omuauf_lfve&q=water接口
{
"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 - water",
"totalResults": "11580000000",
"searchTerms": "water",
"count": 1,
"startIndex": 1,
"inputEncoding": "utf8",
"outputEncoding": "utf8",
"safe": "off",
"cx": "017576662512468239146:omuauf_lfve"
}
],
"nextPage": [
{
"title": "Google Custom Search - water",
"totalResults": "11580000000",
"searchTerms": "water",
"count": 1,
"startIndex": 2,
"inputEncoding": "utf8",
"outputEncoding": "utf8",
"safe": "off",
"cx": "017576662512468239146:omuauf_lfve"
}
]
},
"context": {
"title": "CS Curriculum",
"facets": [
[
{
"anchor": "Lectures",
"label": "lectures",
"label_with_op": "more:lectures"
}
],
[
{
"anchor": "Assignments",
"label": "assignments",
"label_with_op": "more:assignments"
}
],
[
{
"anchor": "Reference",
"label": "reference",
"label_with_op": "more:reference"
}
]
]
},
"searchInformation": {
"searchTime": 0.380675,
"formattedSearchTime": "0.38",
"totalResults": "11580000000",
"formattedTotalResults": "11,580,000,000"
},
"items": [
{
"kind": "customsearch#result",
"title": "Projects: Material Based Splashing of Water Drops - CAVE",
"htmlTitle": "Projects: Material Based Splashing of Water Drops - CAVE",
"link": "https://www1.cs.columbia.edu/CAVE/projects/mat_splash/matsplash.php",
"displayLink": "www1.cs.columbia.edu",
"snippet": "In general, the distribution of droplets of a splash depends on the drop size and velocity; the surface roughness, rigidity, and wetness; and the angle of ...",
"htmlSnippet": "In general, the distribution of droplets of a splash depends on the drop size and velocity; the surface roughness, rigidity, and wetness; and the angle of ...",
"cacheId": "MmD3HwYnsbMJ",
"formattedUrl": "https://www1.cs.columbia.edu/CAVE/projects/mat_splash/matsplash.php",
"htmlFormattedUrl": "https://www1.cs.columbia.edu/CAVE/projects/mat_splash/matsplash.php",
"pagemap": {
"cse_thumbnail": [
{
"src": "https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQ1-s5F6RFCTzoWahOEFmCmscY8RmGuUGoPADYZ66rkQIDA8Y9oEZkNFuA",
"width": "388",
"height": "130"
}
],
"cse_image": [
{
"src": "https://www1.cs.columbia.edu/CAVE/projects/mat_splash/images/main_image.png"
}
]
},
"labels": [
{
"name": "assignments",
"displayName": "Assignments",
"label_with_op": "more:assignments"
}
]
}
]
}
我保存响应,并尝试在本地处理该响应。我想从CSE_IMAGE中获取值,但是如果我用我的函数加载json文件,则页面Map的值为空,例如,索引0上的另一个键值可用。我的模型是通过QuickType生成的,所以它应该是正确的。下面是我的解析json函数:
static func parseJson() {
guard let path = Bundle.main.path(forResource: "response", ofType: "json") else {
return
}
let url = URL(fileURLWithPath: path)
var result: TotalResults?
do {
let jsonData = try Data(contentsOf: url)
result = try JSONDecoder().decode(TotalResults.self, from: jsonData)
if let result = result {
print(result)
} else {
print("Failed to pase")
}
}
catch {
print("Error (error)")
}
}
1条答案
按热度按时间dldeef671#
试着这样做,稍微修改一下https://app.quicktype.io/生成的代码(对我来说很好):
注意,您仍然需要查看服务器文档,以确定哪些属性是可选的。
您还可以执行以下操作: