Swift3 and JSON



我试图解析JSON从我的服务器,但我得到奇怪的行为。

我的网络处理代码:

import Foundation

// Input: URLRequest
// Output: returns JSON or raw data

public let DANetworkingErrorDomain = "(Bundle.main.bundleIdentifier!).NetworkingError"
public let MissingHTTPResponseError: Int = 10
public let UnexpectedResponseError: Int = 20
class NetworkProcessing{
let request: URLRequest
lazy var configuration: URLSessionConfiguration = URLSessionConfiguration.default
lazy var session: URLSession = URLSession(configuration: self.configuration)

init(request: URLRequest) {
    self.request = request
}

//Construct a URLSEssion and download data and return the data
// This is multi Threading
typealias JSON = [String : Any]
typealias JSONHandler = (JSON?, HTTPURLResponse?, Error?) -> Void
typealias DataHandler = (Data?, HTTPURLResponse?, Error?) -> Void

func downloadJSON(completion: @escaping JSONHandler){
    let dataTask = session.dataTask(with: self.request) {
        (data, response, error) in
        //Off the main Thread
        //Error: missing http response
        guard let httpResponse = response as? HTTPURLResponse else {
            let userInfo = [NSLocalizedDescriptionKey : NSLocalizedString("Missing HTTP Response", comment: "")]
            let error = NSError(domain: DANetworkingErrorDomain, code: MissingHTTPResponseError, userInfo: userInfo)
            completion(nil, nil, error as Error)
            return
        }

        //There was a response but no data
        if data == nil {
            if let error = error{
                completion(nil, httpResponse, error)
            }
          //We have some data
        }else{
            switch httpResponse.statusCode{
            case 200:
                //Everything is good Parse the JSON into Foudation Object (array, dictionary..)
                do{
                    let json = try JSONSerialization.jsonObject(with: data!, options: []) as? [String : Any]
                    completion(json, httpResponse, nil)
                } catch let error as NSError {
                    // The JSON data isn't correct
                    completion(nil, httpResponse, error)
                }
                break

             // Any other http status code other than 200
            default:
                print ("Recieved HTTP response code: (httpResponse.statusCode) = was not handeled in NetworkProcessing.swift")
                break
            }
        }

    }
    dataTask.resume()
}

// This is raw data not JSON
func downloadData(completion: @escaping DataHandler) {
    let dataTask = session.dataTask(with: self.request) {
        (data, response, error) in
        //Off the main Thread
        //Error: missing http response
        guard let httpResponse = response as? HTTPURLResponse else {
            let userInfo = [NSLocalizedDescriptionKey : NSLocalizedString("Missing HTTP Response", comment: "")]
            let error = NSError(domain: DANetworkingErrorDomain, code: MissingHTTPResponseError, userInfo: userInfo)
            completion(nil, nil, error as Error)
            return
        }

        //There was a response but no data
        if data == nil {
            if let error = error{
                completion(nil, httpResponse, error)
            }
            //We have some data
        }else{
            switch httpResponse.statusCode{
            case 200:
                //Everything is good Parse the JSON into Foudation Object (array, dictionary..)
                completion(data, httpResponse, error)
                break

            // Any other http status code other than 200
            default:
                print ("Recieved HTTP response code: (httpResponse.statusCode) = was not handeled in NetworkProcessing.swift")
                break
            }
        }

    }
    dataTask.resume()
}
}

我这样称呼它:

import UIKit
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    let baseURL = "http://www.example.com/api/"
    let path = "business.php?tag=getBusCategories"
    let urlString = "(baseURL)(path)"
    let url = URL(string: urlString)!
    let urlRequest = URLRequest(url: url)
    let networkProcessing = NetworkProcessing(request: urlRequest)
    networkProcessing.downloadJSON { (json, httpResponse, error) in
        print(json)
        if let dictionary = json {
            if let busCategoriesDict = dictionary["busCategories"] as? [String : Any]{
                let busCatName = busCategoriesDict["busCatName"]
                print("********************(busCatName)*****************")
            }
        }
    }
}
}

然后我在检查器中得到以下输出:

Optional(["busCategories": <__NSArrayI 0x6080000a7440>(
{
    busCatDescription = "Some description Some Description Some Description";
    busCatId = 1;
    busCatName = Accommodation;
},
{
    busCatDescription = "Some description Some Description Some Description";
    busCatId = 3;
    busCatName = "Bars & Restaurants";
},
{
    busCatDescription = "Some description Some Description Some Description";
    busCatId = 17;
    busCatName = Beauty;
},
{
    busCatDescription = "Some description Some Description Some Description";
    busCatId = 4;
    busCatName = Computer;
},
{
    busCatDescription = Description;
    busCatId = 18;
    busCatName = Conference;
},
{
    busCatDescription = "Some description Some Description Some Description";
    busCatId = 6;
    busCatName = Entertainment;
},
{
    busCatDescription = "Some description Some Description Some Description";
    busCatId = 11;
    busCatName = "Pets & Animals";
},
{
    busCatDescription = "Some description Some Description Some Description";
    busCatId = 12;
    busCatName = Services;
},
{
    busCatDescription = "Some description Some Description Some Description";
    busCatId = 10;
    busCatName = Stores;
},
{
    busCatDescription = Description;
    busCatId = 19;
    busCatName = Weddings;
}
)
, "success": 1, "error": 0])

my Problem is here:

  ["busCategories": <__NSArrayI 0x6080000a7440>(
//the JSON looks like this:
{
    "error": false,
    "success": 1,
    "busCategories": [
        {
            "busCatId": "1",
            "busCatName": "Accommodation",
            "busCatDescription": "Some description Some Description Some Description"
        }, {
        "busCatId": "19",
        "busCatName": "Weddings",
        "busCatDescription": "Description"
    }
]
}

我真的不明白为什么iOS不能正确解析JSON,现在我不能引用busCategories

如果我的理解是正确的,

dictionary["busCategories"]

不是[String:Any],而是[[String:Any]],换句话说,它是一个字典数组,而不是字典,因此

if let busCategoriesDict = dictionary["busCategories"] as? [String : Any]

永远不会成功

不能正确解析JSON的不是iOS。这是你的。: -)

从输出

可以看到

可选(["busCategories": <__NSArrayI 0x6080000a7440>

busCategories是一个数组

使用您的类型别名JSON,使其更清晰。

if let dictionary = json {
    if let busCategoriesArray = dictionary["busCategories"] as? [JSON] {
        for busCategory in busCategoriesArray {
           let busCatName = busCategory["busCatName"] as! String
           print(busCatName)
        }
    }
}

最新更新