如何快速解析字符串

时间:2019-11-03 11:00:31

标签: json swift string dictionary

我的json是这样的:

"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\"}]}]",
......

在这里我可以得到bcustomerparms。但是在这里,我需要bcustomerparms:在一个virable中的paramName值,例如(Account Number / User Name)。为此,我编写了如下代码,但是我无法获得Vairable中的Account Number / User Name。

请在下面的代码中帮助我:

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)") 
 }

//在这里,我正在使用bcustomerparms

  

biller customrParams可选([{“ paramName”:“连接ID”,“ dataType”:“ ALPHANUMERIC”,“可选”:“ false”,“ minLength”:“ 8”,“ maxLength”:“ 10”} ])

但是在这里我只希望paramName值如何获取该值。请帮我上面的代码。

1 个答案:

答案 0 :(得分:2)

bcustomerparms的值是一个字符串,而不是字典的数组,您可以尝试

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"])
}