ios - Swift:JSON 解析未显示在 UILabel 中,但可以使用 Alamofire 在输出窗口中看到
问题描述
我是编码新手,似乎没有显示每日报价UILabel
。使用http://quotes.rest/qod.json
输出控制台显示结果,但UILabel
. 任何帮助是极大的赞赏。
以下是来自网站的 JSON 格式:
{
"success": {
"total": 1
},
"contents": {
"quotes": [
{
"quote": "You make a living by what you earn",
"length": "69",
"author": "Winston Churchill",
"tags": [
"inspire"
],
"category": "inspire",
"id": "XZiOy4u9_g4Zmt7EdyxSIgeF"
}
],
"copyright": "2017-19 theysaidso.com"
}
}
我用来解析上述 JSON 的 Swift 代码是:
import Alamofire
import SwiftyJSON
class DailyQuotes {
private var _quotes: String!
private var _author: String!
var quotes: String
{
if _quotes == nil
{
_quotes = ""
}
return _quotes
}
/// Downloading Current Weather Data
func downloadQuotes(completed: @escaping DownloadComplete){
Alamofire.request(QuotesDaily).responseJSON
{ (response) in
let result = response.result
print(result.value)
let json = JSON(result.value!)
self._quotes = json["contents"]["quotes"]["quote"].stringValue
completed()
}
}
}
在ViewConrtroller.swift
:
func updateQuotesUI()
{
quoteLabel.text = dailyQuotes.quotes
}
解决方案
引号是一个数组
let arr = json["contents"]["quotes"].arrayValue
self._quotes = arr[0]["quote"].stringValue
推荐阅读
- javascript - Angular Universal 不呈现来自 API 请求的数据
- r - 传单和闪亮的 html 小部件错误呈现
- javascript - 如何在 angularjs 中获取子元素
- http - 如何在 HTTPie 中使用 PUT 上传文件
- php - 使用 PHPExcel 库将 Html 表导出到 Excel 文件时出现乱码
- java - 想要在 spring rest API 中发送文件以及其他一些值
- hyperledger-fabric - 在安装 generator-hyperledger-composer 后的 ubntu 中。generator-hyperledger-composer:找不到命令
- spring - 仅使用 spring boot 依赖注入
- json-deserialization - 虽然反序列化来自api“https://jsonplaceholder.typicode.com/posts”的json字符串
- c# - 如何在 xamarin ios 中自动调整 UITableView 的行高