简体   繁体   中英

Swift: Parsing array elements from JSON request

I am getting a JSON response from http request. After getting it, it has arrays and trying to parse.

Swift code:

if let finalResponse = parseJSON["visuals"] as? [String: Any] {


                let balanceResponse = finalResponse ["balanceList"] as? AnyObject
                print("balanceResponse::   \(balanceResponse)")

                let dateResponse = finalResponse ["dateList"] as? AnyObject
                print("dateResponse::   \(dateResponse)")

print log:

balanceResponse::   Optional(<__NSArrayM 0x17404b880>(
{
    data =     (
        "32872.23",
        "38814.87",
        "38915.85"
    );

}
dateResponse::   Optional(<__NSArrayM 0x17005a4f0>(
Apr 26, 2017,
Jun 10, 2017,
Jul 26, 2017
)

How to access 3 values in data array and 3 date values from dateResponse

dateResponse and balanceResponse will return you a new array. So just treat them as any other array and access the element using index and fill your models.

Other simple way I will recommend you to use third party Library like ObjectMapper or SwiftyJson it will less your work and they are very much stable solutions.

UPDATE:

let balanceResponse = finalResponse ["balanceList"] as? AnyObject
let data = balanceResponse[“data”]
let firstValue = data[0]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM