简体   繁体   中英

Problems parsing JSON with SwiftyJSON

I am trying to parse the data below with SwiftyJSON :

{
"state": {
    "on": true,
    "bri": 100,
    "alert": "none",
    "mode": "homeautomation",
    "reachable": true
},
"swupdate": {
    "state": "noupdates",
    "lastinstall": "2018-03-10T10:39:45"
},
"type": "Dimmable light",
"name": "Hue white lamp 1",
"modelid": "LWB010",
"manufacturername": "Philips",
"productname": "Hue white lamp",
"capabilities": {
    "certified": true,
    "control": {
        "mindimlevel": 5000,
        "maxlumen": 806
    },
    "streaming": {
        "renderer": false,
        "proxy": false
    }
},
"config": {
    "archetype": "classicbulb",
    "function": "functional",
    "direction": "omnidirectional"
},
"uniqueid": "00:17:99:f1:03:c4:0e:8b-2e",
"swversion": "1.29.0_r21169",
"swconfigid": "FF7681C5",
"productid": "Philips-LWB010-4-A19DLv4"
}

I successfully manage to print the whole data with the following code:

let json = JSON(value)
print(json)

However, I get null whenever I try to access the internal structure; eg,

print(json["state"])
print(json["swupdate"])
print(json["swconfigid"])

The full function receiving the data is the following:

func getStatusRequest() {
    // getStatusRequest
    // retrieve current status of lamp-1

    Alamofire.request(url, parameters: authParameters).validate().responseJSON { response in
        switch response.result {

        // handle success
        case .success (let value):
            self.infoLabel.textColor = UIColor.green // change lable color
            self.infoLabel.text = "Connection established"

            let json = JSON(value)
            print(json)
            // PARSE JSON HERE



        // handle failure
        case .failure(let error):
            self.disableAll() // disable GUI
            self.infoLabel.textColor = UIColor.red // change lable color

            // print error message
            if let errorCode = response.response?.statusCode {
                switch errorCode {
                case 401: self.infoLabel.text = "[\(errorCode)] Unauthorised access."
                case 403: self.infoLabel.text = "[\(errorCode)] Access denied."
                case 502: self.infoLabel.text = "[\(errorCode)] Service down."
                default: self.infoLabel.text = "[\(errorCode)] Interal error."
                }
            }
            else {
                self.infoLabel.text = "[error] Server down."
                debugPrint("DEBUG: \(error)") // debug print
            }
        }
    }
}

I am using Mac OS 10.13.3, Xcode 9.2, and SwiftyJSON 4. What am I doing wrong?

If the source is a string you have to use the initializer

let json = JSON(parseJSON: value)

The initializer

public init(_ object: Any)

does not parse a String into JSON, instead use init(parseJSON: String)

Note: The Codable prototocol in Swift 4 makes SwiftyJSON as parser obsolete.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM