简体   繁体   English

Swift:从JSON请求解析数组元素

[英]Swift: Parsing array elements from JSON request

I am getting a JSON response from http request. 我收到来自http请求的JSON响应。 After getting it, it has arrays and trying to parse. 获取后,它具有数组并尝试解析。

Swift code: SWIFT代码:

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访问数据数组中的3个值和3个日期值

dateResponse and balanceResponse will return you a new array. dateResponsebalanceResponse将返回一个新数组。 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. 其他简单的方法,我建议您使用第三方库(例如ObjectMapper或SwiftyJson),它将减少您的工作量,并且它们是非常稳定的解决方案。

UPDATE: 更新:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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