簡體   English   中英

如何使用Alamofire在Swift 3中解析JSON?

[英]How to Parse JSON in Swift 3 using Alamofire?

我正在使用Swift3。到目前為止,我實際上還沒有使用過JSON。 我面臨着這個問題,我只能解析數據直到query 之后,沒有數據被解析。 請幫助我知道我在這里做錯了什么。

JSON代碼:

{
  "batchcomplete": "",
  "continue": {
    "gpsoffset": 10,
    "continue": "gpsoffset||"
  },
  "query": {
    "pages": {
      "445066": {
        "pageid": 445066,
        "ns": 0,
        "title": "RoboCop",
        "index": 3,
        "thumbnail": {
          "source": "https://upload.wikimedia.org/wikipedia/en/thumb/1/16/RoboCop_%281987%29_theatrical_poster.jpg/32px-RoboCop_%281987%29_theatrical_poster.jpg",
          "width": 32,
          "height": 50
        }
      },
      "25781": {
        "pageid": 25781,
        "ns": 0,
        "title": "Robot",
        "index": 1,
        "thumbnail": {
          "source": "https://upload.wikimedia.org/wikipedia/commons/thumb/0/05/HONDA_ASIMO.jpg/37px-HONDA_ASIMO.jpg",
          "width": 37,
          "height": 50
        }
      },
      "2629669": {
        "pageid": 2629669,
        "ns": 0,
        "title": "Robot-assisted surgery",
        "index": 8,
        "thumbnail": {
          "source": "https://upload.wikimedia.org/wikipedia/commons/thumb/0/0d/Laproscopic_Surgery_Robot.jpg/34px-Laproscopic_Surgery_Robot.jpg",
          "width": 34,
          "height": 50
        }
      },
      "1527386": {
        "pageid": 1527386,
        "ns": 0,
        "title": "Robot Chicken",
        "index": 9
      },
      "364093": {
        "pageid": 364093,
        "ns": 0,
        "title": "Robot Wars (TV series)",
        "index": 2
      },
      "3977472": {
        "pageid": 3977472,
        "ns": 0,
        "title": "Robot competition",
        "index": 10
      },
      "26333": {
        "pageid": 26333,
        "ns": 0,
        "title": "Robotech",
        "index": 5,
        "thumbnail": {
          "source": "https://upload.wikimedia.org/wikipedia/en/thumb/9/99/RobotechTitle1985.jpg/50px-RobotechTitle1985.jpg",
          "width": 50,
          "height": 38
        }
      },
      "20903754": {
        "pageid": 20903754,
        "ns": 0,
        "title": "Robotics",
        "index": 4,
        "thumbnail": {
          "source": "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c5/Shadow_Hand_Bulb_large.jpg/33px-Shadow_Hand_Bulb_large.jpg",
          "width": 33,
          "height": 50
        }
      },
      "893808": {
        "pageid": 893808,
        "ns": 0,
        "title": "Robots (2005 film)",
        "index": 7
      },
      "101673": {
        "pageid": 101673,
        "ns": 0,
        "title": "Robots exclusion standard",
        "index": 6
      }
    }
  }
}

SWIFT代碼:

 var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options:.mutableContainers) as? JSONStandard
        print(readableJSON)
            if let query = readableJSON?["query"] as? JSONStandard{

                print(query," Here!! ")

                if let pages = readableJSON?["pages"] as? [JSONStandard]{

                    print(pages," Check this! ")


                    for i in 0..<pages.count{

                        let page = pages[i]

                        let id = page ["id"] as! String

                        //titles.append(id)

                        self.tableView.reloadData()

                    }
                }
            }

正如您在塊pages看到的那樣,每個新塊都被聲明為沒有任何標識符,那么如何調用該特定塊?

關鍵pages的值是字典,而不是數組,它是從query派生的。

請閱讀JSON: []是數組{}是字典。

而且沒有鍵id ,它是pageid ,值是一個Int (沒有雙引號)。

         if let pages = query["pages"] as? JSONStandard {
                print(pages," Check this! ")
                for (_, page) in pages {
                    let id = page ["pageid"] as! Int
                    print(id)
                    titles.append("\(id)")
                }
                self.tableView.reloadData() // don't reload the table view in the loop.
            }

或–更容易–取得字典的鍵,它也是id號(這里確實是String

         if let pages = query["pages"] as? JSONStandard {
                print(pages," Check this! ")
                for (id, page) in pages {
                    print(id)
                    titles.append(id)
                }
                self.tableView.reloadData()
            }

請注意,字典始終是無序的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM