Swift - 使用swiftyJSON根据字典中的值查找字典数组的索引

时间:2015-03-24 10:17:18

标签: ios arrays swift dictionary swifty-json

我对编程很新,更不用说Swift了。我理解以前编程尝试中的一些概念,所以我比以前更进一步。如果我不清楚我需要什么,我会道歉。

我正在提取警报列表的JSON数据,我正在尝试使用swiftyJSON解析数据,我认为这样做没问题但是我遇到了尝试从数组中的某些字典中获取一些数据的问题,不幸的是,在这个档案里面的字典里面是电子邮件地址,不同的字典在彼此之间使用相似的键。

我将向您展示我的struct,fucntion和JSON数据。请帮我抓住电子邮件地址,服务 - 其ID和标签。此外,在显示的数据中可能有多个服务,我需要捕获所有服务。

这是JSON数据:

{
"hasNext": false,
"data": [
    {
        "status": [
            1,
            "READ"
        ],
        "resolutionStatus": [
            0,
            "OPEN"
        ],
        "description": "There is some description here",
        "title": "Some Activity",
        "entities": [
            {
                "view_name": "audits",
                "type": "link",
                "parameters": {
                    "orgUnit": "/"
                },
                "label": "/"
            },
            {
                "type": "user",
                "id": "hidden@hidden.com",
                "label": "hidden@hidden.com"
            },
            {
                "type": "service",
                "id": 6666,
                "label": "someService"
            },
            {
                "type": "service",
                "id": 7777,
                "label": "anotherService"
            }
        ],
        "stories": [
            5
        ],
        "date": "2014-12-10T23:46:28.067000Z",
        "audits": [
            "ljBhqKQVOF9w",
            "pISQyT9iy9w",
            "oynGf2_CIw"
        ],
        "_id": "54fdad0dfd",
        "id": [
            14683,
            "ALERT_SOME_ACTIVITY"
        ],
        "severity": [
            5,
            "HIGH"
        ]
    }

这是我的结构:

    struct AlertModel: Printable {
    let alertUser: String?
    let alertService: String?
    let alertTitle: String?
    let alertReadStatus: String?
    let alertResolutionStatus: String?
    let alertDescription: String?
    let alertEntities: Array <String> = []
    let alertDate: String?
    let alertAudits: Array <String> = []
    let alertId: String?
    let

     alertSeverity: String?

      // change description to print to console
         var description: String {
            return "User: \(alertUser)\nService: \(alertService)\nTitle: \(alertTitle!)\nRead Status: \(alertReadStatus!)\nResolution Status: \(alertResolutionStatus!)\nDescription: \(alertDescription!)\nDate: \(alertDate!)\nAlert ID: \(alertId!)\nSeverity: \(alertSeverity!)\n******************************************\n"
        }

        init(alertUser: String?, alertService: String?, alertTitle: String?, alertReadStat

us: String?, alertResolutionStatus: String?, alertDescription: String?/*, alertEntities: Array<String>*/, alertDate: String?/*, alertAudits: Array<String>*/, alertId: String?, alertSeverity: String?) {
        self.alertUser = alertUser
        self.alertService = alertService
        self.alertTitle = alertTitle
        self.alertReadStatus = alertReadStatus
        self.alertResolutionStatus = alertResolutionStatus
        self.alertDescription = alertDescription
        //self.alertEntities = alertEntities
        self.alertDate = alertDate
        //self.alertAudits = alertAudits
        self.alertId = alertId
        self.alertSeverity = alertSeverity
    }

这里是功能:

let jsonAlert = JSON(data: jsonAlertObject)
    if let alertArray = jsonAlert["data"].array {
        var alerts = [AlertModel]()
        for alertDict in alertArray {
            let alertTitle: String? = alertDict["title"].stringValue
            let alertReadStatus: String? = alertDict["status"][1].stringValue
            let alertResolutionStatus: String? = alertDict["resolutionStatus"][1].stringValue
            let alertDescription: String? = alertDict["description"].stringValue
            let alertDate: String? = alertDict["date"].stringValue
            let alertId: String? = alertDict["_id"].stringValue

            // Need to grab the type and label from each dictionary in the array of entities
            let alertEntitiesArray: Array? = alertDict["entities"].arrayObject
            var arrayIndex = 0
            var entitiesDict = ["" : ""]
            while arrayIndex < alertEntitiesArray?.count {
                entitiesDict[alertDict["entities"][arrayIndex]["type"].stringValue] = alertDict["entities"][arrayIndex]["label"].stringValue
                arrayIndex++
            }
            let alertService: String? = entitiesDict["service"]
            let alertUser: String? = entitiesDict["user"]
            let alertSeverity: String? = alertDict["severity"][1].stringValue

            let alert = AlertModel(alertUser: alertUser, alertService: alertService, alertTitle: alertTitle, alertReadStatus: alertReadStatus, alertResolutionStatus: alertResolutionStatus, alertDescription: alertDescription, alertDate: alertDate, alertId: alertId, alertSeverity: alertSeverity)
            alerts.append(alert)
            var alertsDictionaryByID = [alertId!: alert]

        }
        println(alerts)
    }

正如您所看到的,JSON数据有几个层次。我没有问题获取数据并将其拉出来。问题是&#34;实体&#34;数组可能并不总是包含相同的数据。它有多个服务,它可能没有电子邮件地址,它可能有一个完全不同的数据集为数组的第一个值。

我想要收到电子邮件地址。如果我能找到一种方法来搜索&#34;用户&#34;与字典数据一样,然后在找到它时会返回数组索引值以便能够直接引用它,因为我永远不会知道索引号是否是用户值的一部分。

我希望我能说清楚,有人可以帮助我。 ---我的下一步将使用每个单独的警报填充listView。

2 个答案:

答案 0 :(得分:0)

我也使用SwiftyJSON,但使用NSURLConnection很容易。喜欢这种方法。

//MARK: - NSURLConnection Delegate methods


    var responseData : NSMutableData = NSMutableData()

    func connection(connection: NSURLConnection!, didReceiveResponse response: NSURLResponse!)
    {
        self.responseData.length = 0
    }

    func connection(connection: NSURLConnection!, didReceiveData data: NSData!)
    {
        self.responseData.appendData(data)
    }


    func connectionDidFinishLoading(connection: NSURLConnection!)
    {
        if let responseDatas = responseData as NSData? {

            if let jsonResult : NSDictionary = NSJSONSerialization.JSONObjectWithData(responseDatas, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary? {


                if let dataArray = jsonResult.valueForKeyPath("data") as? NSArray 
                 {

                  if let entitiesArray = dataArray.valueForKeyPath("entities") as? NSArray 
                 {

                 if let firstArray = dataArray[0] 
                 {
                     // access your viewName and types here
                 }                    

                     // Like this method, convert all your parameters and access it.

                }                    
                }
                else
                {
               // Do something here
                }

            }
            else
            {
                 // Do something here

            }
        }
        else
        {
             // Do something here

        }



    }

    func connection(connection: NSURLConnection!, didFailWithError error: NSError!)
    {
     // Do something here
    }

答案 1 :(得分:0)

我最终找到了SwiftyJSON和Alamofire来大力协助网络和JSON序列化。

就我在数组中的字典问题而言,我最终创建了一个函数,它迭代遍历实体数组,单独查看每个字典,然后根据键=&#34;类型&#34;执行一个Switch语句。确定它是否是&#34;用户&#34;字典或&#34;服务&#34;字典然后结合&#39;类型&#39; value,如果是用户或服务,则表示&#34;标签&#34;的值,即用户名或服务名称,并创建一个新的字典,然后我可以引用它回到我的数据模型中。

class func retrieveDataFromEntitiesArray (alertDict: JSON) -> (entitesDict: Dictionary<String, String>, servicesArray: [String]) {
    // Need to create an array object, instead of a JSON object, of the entities array to be able to get a count to run the while against.
    var arrayIndex = 0
    var entitiesDict: Dictionary<String, String> = [:]
    var alertEntitiesArray = alertDict.arrayObject
    var servicesArray = [String]()
    while arrayIndex < alertEntitiesArray?.count {
        var dictKey = alertDict[arrayIndex]["type"].string
        switch (dictKey!) {
        case "user":
            entitiesDict[alertDict[arrayIndex]["type"].stringValue] = alertDict[arrayIndex]["label"].stringValue
        case "service":
            servicesArray.append(alertDict[arrayIndex]["label"].stringValue)
        case "policyRule":
            entitiesDict[alertDict[arrayIndex]["type"].stringValue] = alertDict[arrayIndex]["label"].stringValue
        default:
            println("Nothing Here")
        }
        arrayIndex++
    }
    return (entitiesDict, servicesArray)
}