0

I'm fairly new to Swift and trying to get a JSON array that comes back from Alamofire into a normal Swift array.

var dpoParamArr     = JSON([])
//Alamofire code here and I get the value back below which is Parameters
self.dpoParamArr    = JSON(dictMain)["Parameters"] 

print(self.dpoParamArr.arrayValue.map({($0.dictionaryValue["name"]?.stringValue)!}))
print(self.dpoParamArr.arrayValue.map({($0.dictionaryValue["value"]?.stringValue)!}))

This is the print result: ["PAY_REQUEST_ID", "CHECKSUM"] ["5FB1DDBB-D637-3DC3-C0AF-288EFF98012C", "4FD856821E0F6D238F8346175227FF04"]

How do I get my array to have a key and value

 "Parameters": [
    {
      "name": "PAY_REQUEST_ID",
      "value": "94DEE72B-F75C-453F-1280-F1B26BBFD98E"
    },
    {
      "name": "CHECKSUM",
      "value": "9171405E05C9C9B7D4B6FF497FC4AE50"
    }
  ]

My end goal is to get the name and value variables for both JSON nodes.

9
  • "I get my array to have a key and value" Are you sure you want an array, not a array of Dictionary? Could you show, if you created it yourself, what it would be? Commented Dec 21, 2020 at 11:35
  • Sorry but not clear on the terminology. All I want to do is for loop through my JSON(dictMain)["Parameters"] to get the name and value for the two JSON nodes. Commented Dec 21, 2020 at 11:42
  • I need the values for a https form: postFormHtml += "<input type='hidden' name='PAY_REQUEST_ID' value='044CC083-615E-0439-7F42-BD1959FA427A'></input>" postFormHtml += "<input type='hidden' name='CHECKSUM' value='3CFC33AD6B713A57E783F53FF0A96CE2'></input>" Commented Dec 21, 2020 at 11:43
  • self.dpoParamArr.forEach { postFormHtml += "<input type='hidden' name='\($0.key)' value='\($0.value)'></input>" } should do the trick then. But it'd be nice to add your end-goal to your question by editing it, not only in comment. Commented Dec 21, 2020 at 11:45
  • I get an error "Value of tuple type '(String, JSON)' has no member 'key'" Commented Dec 21, 2020 at 11:50

1 Answer 1

2

It's look like you are using Alamofire with SwiftyJSON. Below is some simple way to convert json to swift array.

1.Convert Json array to swift array.

            if let swiftArray = self.dpoParamArr.arrayObject as? [[String:Any]] {
                
                // swiftArray is array of dictionary
                for item in swiftArray {

                    // item is a dictionary and you will get its value by key 
                    // here how to get value by key
                    
                    let name = item["name"] as? String
                    let value = item["value"] as? String
                    
                    //printing the values
                    print("Name is: ", name)
                    print("Value is: ", value)
                    print("=========================")
                }
            }
            
  1. You can also use like below

             for item in self.dpoParamArr.arrayValue {
    
                 //here how to get value by key
                 let name = item["name"].stringValue
                 let value = item["value"].stringValue
    
                 //printing the values
                 print("Name is: ", name)
                 print("Value is: ", value)
                 print("=========================")
             }
    

You can use SwiftyJSON for parsing. SwiftyJSON is a simplified JSON parsing library that gives you clearer syntax than the built-in iOS libraries. To detail information please read its document

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.