0
"documenttypes": [{
        "documentname": "DNC Format",
        "mandatory": "0",
        "limitnumber": "2",
        "supportingfiles": {
            "document": [{
                "type": "DNC Format",
                "displayname": "DNC_Format"
            }]
        }
    },

    {
        "documentname": "Joint Life Addendum",
        "mandatory": "0",
        "limitnumber": "3",
        "supportingfiles": {
            "document": [{
                "type": "Joint Life Addendum",
                "displayname": "Joint_Life_Addendum"
            }]
        }
    },
    {
        "documentname": "Age proof of Child",
        "mandatory": "0",
        "limitnumber": "3",
        "supportingfiles": {
            "document": [{
                "type": "Age proof of Child",
                "displayname": "Age_proof_of_Child"
            }]
        }
    },
    {
        "documentname": "Permanent Address Proof of Proposer",
        "mandatory": "0",
        "limitnumber": "4",
        "supportingfiles": {
            "document": [{
                "type": "AADHAR Card",
                "displayname": "AADHAR_Card"
            },
            {
                "type": "Aadhar Card letter",
                "displayname": "Aadhar_Card_letter"
            },
            {
                "type": "KMBL KYC ",
                "displayname": "KMBL_KYC "
            },
            {
                "type": "Driving License",
                "displayname": "Driving_License"
            }
            ]
        }
    }]

我将类型键提取到 typeArray 中,但我需要提取为数组数组:

if let personArray = jsonResult.value(forKey: "documenttypes") as? NSArray {
    for (_, element) in personArray.enumerated() {
    if let element = element as? NSDictionary {
        let id = element.value(forKey: "documentname") as! String
        self.documentnameArr.append(id)

        let files = element.value(forKey: "supportingfiles") as! NSDictionary

        if let personArray = files.value(forKey: "document") as? NSArray {
            for (_, element) in personArray.enumerated() {
                if let element = element as? NSDictionary {

                    if let myArr = element.value(forKey: "type"){

                        self.typeArr.append(myArr as! String)
                    }
                }
            }
        }
    }
}

我正在获取所有类型键值,typeArr但我需要像数组数组这样的值:

[["DNC FORMAT"],["Joint Life Addendum"],[儿童年龄证明],[AADHAR Card,Aadhar Card letter,KMBL KYC ,Driving License]]

4

3 回答 3

0

这是我的代码:

var documentnameArr: [String] = []
var typeArr:[[String]] = []
if let personArray = jsonResult["documenttypes"] as NSArray? {
    for (_, element) in personArray.enumerated() {
    if let element = element as? NSDictionary {
        let id = element.value(forKey: "documentname") as! String
        documentnameArr.append(id)
        let files = element.value(forKey: "supportingfiles") as! NSDictionary
        if let personArray = files.value(forKey: "document") as? NSArray {
            var arrayInDocument: [String] = []
            for (_, element) in personArray.enumerated() {
                if let element = element as? NSDictionary {
                    if let myArr = element.value(forKey: "type"){
                        arrayInDocument.append(myArr as! String)
                    }
                }
            }
            typeArr.append(arrayInDocument)
        }
    }
    }
}
print(documentnameArr)
print(typeArr)
于 2020-04-28T09:33:36.530 回答
0

JSON parsing will be more straightforward if you use swift Codable. You never need to iterate through JSON keys in this way, You only just need to parse it to appropriate struct and classes based on your need.

For example in your case, you need to create three structs for your JSON types: documenttypes, supportingfiles and document.

In case you need these types to be only decoded you can just conform to Decodable.

You can read more about pasign JSON using Codable in this article provided by John Sundel

于 2020-04-28T09:56:15.167 回答
0

您可以使用 Cocoapods 的“SwiftyJSON”pod,强烈推荐。

于 2020-04-28T12:57:20.907 回答