Swift
Swift

Reputation: 1184

How to parse string in swift

My json is like this:

"billerdetails": [
{
"id": "1",
"bname": "ACT Fibernet",
"bcustomerparms": "[{\"paramName\":\"Account Number/User Name\",\"dataType\":\"ALPHANUMERIC\",\"optional\":\"false\",\"minLength\":\"1\",\"maxLength\":\"50\"}]",
"breponseParams": "[{\"amtBreakupList\":[{\"amtBreakup\":\"BASE_BILL_AMOUNT\"}]}]",
......

Here i am able to get bcustomerparms. but here i need bcustomerparms: paramName value like (Account Number/User Name) in one virable.. for that i have written code like below but i am unable to get Account Number/User Name in vairable.

Please help me in below code:

do{
let jsonObj = try JSONSerialization.jsonObject(with: respData, options: .allowFragments) as! [String: Any]
//print("the all make payment json is \(jsonObj)")

let billerdetailsArray = jsonObj["billerdetails"] as! [[String: Any]]

for billerdetail in billerdetailsArray {

    self.categoryName = billerdetail["bname"] as? String
    var customrParams = billerdetail["bcustomerparms"]
    print("biller customrParams \(customrParams)") 
 }

// here i am getting bcustomerparms

biller customrParams Optional([{"paramName":"Connection ID","dataType":"ALPHANUMERIC","optional":"false","minLength":"8","maxLength":"10"}])

but here i want only paramName value how to get that value. please help me in the above code.

Upvotes: 1

Views: 100

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100533

bcustomerparms value is a string not an array of dictionaries , You can try

let customrParams = billerdetail["bcustomerparms"] as! String
let res = try JSONSerialization.jsonObject(with:Data(customrParams.utf8)) as! [[String: Any]]
for item in res {
  print(item["paramName"])
}

Upvotes: 2

Related Questions