json - 从 GoogleApiBooks 解析 JSON

标签 json swift xcode alamofire swifty-json

我正在尝试解析此 JSON,但它不起作用。

这是 JSON:

{
  "kind": "books#volumes",
  "totalItems": 1,
  "items": [
    {
      "kind": "books#volume",
      "id": "uCHmPQAACAAJ",
      "etag": "aBZ3KnoRsq4",
      "selfLink": "https://www.googleapis.com/books/v1/volumes/uCHmPQAACAAJ",
      "volumeInfo": {
        "title": "Psicologia delle folle",
        "authors": [
          "Gustave Le Bon"
        ],
        "publishedDate": "2004",
        "industryIdentifiers": [
          {
            "type": "ISBN_10",
            "identifier": "8850206240"
          },
          {
            "type": "ISBN_13",
            "identifier": "9788850206247"
          }
        ],
        "readingModes": {
          "text": false,
          "image": false
        },
        "pageCount": 251,
        "printType": "BOOK",
        "categories": [
          "Psychology"
        ],
        "maturityRating": "NOT_MATURE",
        "allowAnonLogging": false,
        "contentVersion": "preview-1.0.0",
        "language": "it",
      },
      "saleInfo": {
        "country": "IT",
        "saleability": "NOT_FOR_SALE",
        "isEbook": false
      },
      "accessInfo": {
        "country": "IT",
        "viewability": "NO_PAGES",
        "embeddable": false,
        "publicDomain": false,
        "textToSpeechPermission": "ALLOWED",
        "epub": {
          "isAvailable": false
        },
        "pdf": {
          "isAvailable": false
        },
        ,
        "accessViewStatus": "NONE",
        "quoteSharingAllowed": false
      }
    }
  ]
}

这是我正在使用的代码:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    var url = "https://www.googleapis.com/books/v1/volumes?q=isbn9788850206247&key=my-key"


    Alamofire.request(url).responseJSON { (responseData) -> Void in
        if((responseData.result.value) != nil) {
            let swiftyJsonVar = JSON(responseData.result.value!)

            if let resData = swiftyJsonVar["items"].arrayObject {
                self.arrRes = resData as! [[String:AnyObject]]
                print(self.arrRes)
            }
            if self.arrRes.count > 0 {
                self.tableProva.reloadData()
            }
        }
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableProva.dequeueReusableCell(withIdentifier: "cellProva", for: indexPath) as! CellProvaClass
    var dict = arrRes[indexPath.row]
    cell.titleLabel?.text = dict["title"] as? String
    cell.authorLabel?.text = dict["authors"] as? String
    return cell
}

如何解决这个问题?

最佳答案

这是您的 JSON 响应(短格式):

enter image description here

这是数据结构(数组和字典格式): enter image description here

现在您必须访问: titleauthors 所以这是您在 cellforRowAtIndexPath Method 中的值,一个名为 dict 的字典 存储 volumeInfo(字典) 的值:

所以你必须使用这种方式来访问标题:

cell.titleLabel?.text = dict["volumeInfo"]["title"] as? String

//To Fetch Author array
  if let authorArray = dict["volumeInfo"]["authors"] as? NSArray{
          print(authorArray)
   }

然后访问author是一个数组,以便您想要在cell.authorLabel中显示哪个值的索引。

在第二个屏幕截图中,{} 中的内容是字典,[] 是数组

首先尝试显示标题,然后以相同的方式尝试获取作者数组的值并根据您的要求进行设置。

如果有任何其他问题,请随时发表评论。

关于json - 从 GoogleApiBooks 解析 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41075264/

相关文章:

xcode - 如何在 Swift 中删除 SKShapeNode

json - 解析错误:尝试解析数据框中的JSON列时出现“Trailing Garbage”

javascript - 从 JSON 对象中删除数据

WCF 服务未在浏览器中返回任何结果

python - Swift NSSocket 编程/python 教程修复

swift - UItableView 无法快速重新加载数据

ios - 在标签中显示测试时出错

java - 为什么我在执行 doInBackground() 时出现错误

arrays - 如何在 Swift 3 中将十六进制转换为十进制? (没有第三方库和Foundation的自写代码)

c++ - 我是否必须在 Xcode iOS 项目中释放 C 分配的内存?