-1
{
"list": [
    {
        "summary": [
            {
                "IN_AMT": 0,
                "OK_AMT": 0,
                "TR_MONTH": "201906"
            },
            {
                "IN_AMT": 0,
                "OK_AMT": 24256210,
                "TR_MONTH": "201907"
            }
        ],
        "yyyyMm": "201907",
        "list": [
            {
                "ROW_NUM": 1,
                "TR_DT": "20190623",
                "OK_CHK_STS": "0",
                "IN_AMT": 0,
                "COMPX_CHK_STS": "4",
                "IN_CHK_STS": "0",
                "OK_AMT": 0
            },
            {
                "ROW_NUM": 2,
                "TR_DT": "20190624",
                "OK_CHK_STS": "0",
                "IN_AMT": 0,
                "COMPX_CHK_STS": "4",
                "IN_CHK_STS": "0",
                "OK_AMT": 0
            },
            {
                "ROW_NUM": 3,
                "TR_DT": "20190625",
                "OK_CHK_STS": "0",
                "IN_AMT": 0,
                "COMPX_CHK_STS": "4",
                "IN_CHK_STS": "0",
                "OK_AMT": 0
            },
            {
                "ROW_NUM": 4,
                "TR_DT": "20190626",
                "OK_CHK_STS": "0",
                "IN_AMT": 0,
                "COMPX_CHK_STS": "4",
                "IN_CHK_STS": "0",
                "OK_AMT": 0
            },
            {
                "ROW_NUM": 5,
                "TR_DT": "20190627",
                "OK_CHK_STS": "0",
                "IN_AMT": 0,
                "COMPX_CHK_STS": "4",
                "IN_CHK_STS": "0",
                "OK_AMT": 0
            }
                ]
    }
}

Hello I am a student studying swift. I am using swift 5. I want to store this data in a model and use it for extraction.

I want to model repeated data and use it in a TableView.

How should I get the elements of the objects in the array?

damores
  • 2,251
  • 2
  • 18
  • 31
duny
  • 3
  • 1

1 Answers1

3

In general I would approach this by following these steps:

  1. Make sure you are handling a valid JSON
  2. Create a model that conforms to the Codable protocol
  3. Decode your JSON and use the data wherever you need it

To start with your JSON is not a valid JSON, you can use online tools to validate it such as JSONLint. In your case you were missing a couple of brackets, here is a valid JSON:

{
"list": [{
    "summary": [{
            "IN_AMT": 0,
            "OK_AMT": 0,
            "TR_MONTH": "201906"
        },
        {
            "IN_AMT": 0,
            "OK_AMT": 24256210,
            "TR_MONTH": "201907"
        }
    ],
    "yyyyMm": "201907",
    "list": [{
            "ROW_NUM": 1,
            "TR_DT": "20190623",
            "OK_CHK_STS": "0",
            "IN_AMT": 0,
            "COMPX_CHK_STS": "4",
            "IN_CHK_STS": "0",
            "OK_AMT": 0
        },
        {
            "ROW_NUM": 2,
            "TR_DT": "20190624",
            "OK_CHK_STS": "0",
            "IN_AMT": 0,
            "COMPX_CHK_STS": "4",
            "IN_CHK_STS": "0",
            "OK_AMT": 0
        },
        {
            "ROW_NUM": 3,
            "TR_DT": "20190625",
            "OK_CHK_STS": "0",
            "IN_AMT": 0,
            "COMPX_CHK_STS": "4",
            "IN_CHK_STS": "0",
            "OK_AMT": 0
        },
        {
            "ROW_NUM": 4,
            "TR_DT": "20190626",
            "OK_CHK_STS": "0",
            "IN_AMT": 0,
            "COMPX_CHK_STS": "4",
            "IN_CHK_STS": "0",
            "OK_AMT": 0
        },
        {
            "ROW_NUM": 5,
            "TR_DT": "20190627",
            "OK_CHK_STS": "0",
            "IN_AMT": 0,
            "COMPX_CHK_STS": "4",
            "IN_CHK_STS": "0",
            "OK_AMT": 0
        }
    ]
}
]
}

The second step is to create a model that conforms to the Codable protocol or better said conforms to both the Decodable and Encodable protocols, since Codable is a typealias.

Again you can try to create your model by yourself but there are tools online that make your life easier, for example QuickType allows you to paste you JSON and get a model straight away:

// MARK: - DunyObject

struct DunyObject: Codable {
let list: [DunyObjectList]
}

// MARK: - DunyObjectList

struct DunyObjectList: Codable {
    let summary: [Summary]
    let yyyyMm: String
    let list: [ListList]
}

// MARK: - ListList

struct ListList: Codable {
    let rowNum: Int
    let trDt, okChkSts: String
    let inAmt: Int
    let compxChkSts, inChkSts: String
    let okAmt: Int

    enum CodingKeys: String, CodingKey {
        case rowNum
        case trDt
        case okChkSts
        case inAmt
        case compxChkSts
        case inChkSts
        case okAmt
    }
}

// MARK: - Summary

struct Summary: Codable {
    let inAmt, okAmt: Int
    let trMonth: String

    enum CodingKeys: String, CodingKey {
        case inAmt
        case okAmt
        case trMonth
    }
}

Lastly you can populate an instance of your model by decoding your JSON, there are plenty of examples online to understand how to safely parse JSON via Codable protocol. As short example you can decode your JSON as follows:

let dunyObject = try? newJSONDecoder().decode(DunyObject.self, from: jsonData)

Now you can use dunyObject to populate your tableViews

SwissMark
  • 1,041
  • 12
  • 21