gpt4 book ai didi

swift - Wunderground API 的部分内容在 Swift 中无法访问

转载 作者:行者123 更新时间:2023-11-30 14:08:50 28 4
gpt4 key购买 nike

我在我的项目中使用 Wunderground API,我想要使用的 API 部分如下所示:

"history": {
"dailysummary": [
{ "date": {
"pretty": "12:00 PM PDT on August 12, 2015",
"year": "2015",
"mon": "08",
"mday": "12",
"hour": "12",
"min": "00",
"tzname": "America/Los_Angeles"
},
"fog":"0","rain":"0","snow":"0","snowfallm":"0.00", "snowfalli":"0.00","monthtodatesnowfallm":"", "monthtodatesnowfalli":"","since1julsnowfallm":"", "since1julsnowfalli":"","snowdepthm":"", "snowdepthi":"","hail":"0","thunder":"0","tornado":"0","meantempm":"26", "meantempi":"79","meandewptm":"16", "meandewpti":"60","meanpressurem":"1014", "meanpressurei":"29.94","meanwindspdm":"9", "meanwindspdi":"5","meanwdire":"","meanwdird":"331","meanvism":"16", "meanvisi":"10","humidity":"","maxtempm":"33", "maxtempi":"91","mintempm":"19", "mintempi":"66","maxhumidity":"78","minhumidity":"34","maxdewptm":"17", "maxdewpti":"62","mindewptm":"15", "mindewpti":"59","maxpressurem":"1016", "maxpressurei":"30.01","minpressurem":"1012", "minpressurei":"29.88","maxwspdm":"24", "maxwspdi":"15","minwspdm":"0", "minwspdi":"0","maxvism":"16", "maxvisi":"10","minvism":"16", "minvisi":"10","gdegreedays":"28","heatingdegreedays":"0","coolingdegreedays":"14","precipm":"0.00", "precipi":"0.00","precipsource":"","heatingdegreedaysnormal":"0","monthtodateheatingdegreedays":"0","monthtodateheatingdegreedaysnormal":"0","since1sepheatingdegreedays":"","since1sepheatingdegreedaysnormal":"","since1julheatingdegreedays":"0","since1julheatingdegreedaysnormal":"17","coolingdegreedaysnormal":"5","monthtodatecoolingdegreedays":"106","monthtodatecoolingdegreedaysnormal":"69","since1sepcoolingdegreedays":"","since1sepcoolingdegreedaysnormal":"","since1jancoolingdegreedays":"600","since1jancoolingdegreedaysnormal":"280" }
]
}

由于某种原因,同时具有“{}”括号和“[]”括号的 dailysummary 无法按照我通常的方式访问,如下所示:

var jsonData = json["history"]["dailysummary"]["fog"] 

如果正常工作,将在我的函数中返回fog值。该功能运行良好;我已经用 API 的其他部分对其进行了测试。 dailysummary 是否需要做一些具体的事情?

最佳答案

您误解了如何调用 json.将 json 美化为如下所示可能会有所帮助:

{
"history": {
"dailysummary": [{
"date": {
"pretty": "12:00 PM PDT on August 12, 2015",
"year": "2015",
"mon": "08",
"mday": "12",
"hour": "12",
"min": "00",
"tzname": "America/Los_Angeles"
},
"fog": "0",
...
}]
}
}

通过上面的内容,dailysummary 是一个包含单个对象的数组,这是非常不言自明的。要访问“雾”,您可以通过 json.history.dailysummary[0].fog

调用它

关于swift - Wunderground API 的部分内容在 Swift 中无法访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31998189/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com