简体   繁体   中英

Json Parsing in swift 3 using Alamofire

I am working in swift 3. I am new to ios. I am trying to parse the json data like

My jsonVlaue is : {
    data =     (
                {
            Password = "@1234";
            UserName = "<null>";
            "___class" = OrderTable;
            "__meta" = "{\"relationRemovalIds\":{},\"selectedProperties\":[\"UserName\",\"created\",\"name\",\"___class\",\"ownerId\",\"updated\",\"objectId\",\"Password\"],\"relatedObjects\":{}}";
            created = 1483525854000;
            name = TestMan;
            objectId = "4316DEBA-78C1-C7BD-FFBC-3CB77D747F00";
            ownerId = "<null>";
            updated = "<null>";
        },
                {
            Password = 123;
            UserName = "<null>";
            "___class" = OrderTable;
            "__meta" = "{\"relationRemovalIds\":{},\"selectedProperties\":[\"UserName\",\"created\",\"name\",\"___class\",\"ownerId\",\"updated\",\"objectId\",\"Password\"],\"relatedObjects\":{}}";
            created = 1483516868000;
            name = tommy;
            objectId = "29155114-C00B-5E1C-FF6F-7C828C635200";
            ownerId = "<null>";
            updated = "<null>";
        }.......

I want only the keyvalue:"name" and that value I want to add in an Array.

I tried to do like that but my app is getting Crash. My code i slike as follows

 func getLoginDetails()
    {
       //https://api.backendless.com/<version>/data/<table-name>/properties

        Alamofire.request( HeadersClass.api.domainName + "OrderTable", method: .get, parameters: nil, encoding: JSONEncoding.default, headers: HeadersClass.allHeaders.headers).responseJSON { response in
            //debugPrint(response)
            if let jsonDict = response.result.value as? NSDictionary {
                print("My jsonVlaue is : \(jsonDict)")

                 let arrayPracticeData: NSArray = jsonDict.value(forKey: "name") as! NSArray

                    print(arrayPracticeData)


            }

        }
}

Can anyone please tell me how to solve this. Thanks in Advance.

First of all in Swift use Swift's native Array and Dictionary instead of NSDictionary and NSArray .

Now to get name you need to get Data array from your JSON response Dictionary . So try something like this.

Alamofire.request( HeadersClass.api.domainName + "OrderTable", method: .get, parameters: nil, encoding: JSONEncoding.default, headers: HeadersClass.allHeaders.headers).responseJSON { response in

     //debugPrint(response)
     if let jsonDict = response.result.value as? [String:Any], 
        let dataArray = jsonDict["data"] as? [[String:Any]] {
           let nameArray = dataArray.flatMap { $0["name"] as? String }
           print(nameArray)
     }
}

Output

["TestMan", "tommy", ...]

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