在Swift iOS9中从Json Data提取值 [英] Extracting values from Json Data in Swift iOS9

查看:82
本文介绍了在Swift iOS9中从Json Data提取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从服务器中检索了以下JSon数据,我想提取值名称并将其作为模型存储在数组或字典中,作为我应用程序中的模型.问题在于返回的自身值是另一种字典的形式.我如何提取频率,描述和数量的值并将其更新到我的表视图中.以下是我从服务器请求中获取的Json数据格式.我是新手,字典和数组的概念让我很困惑

I have the following JSon Data retrieved from the server and i want to extract the value name and store them in an array or dictionary as a model in my app. The issue is that the value returned its self is in a form of another dictionary. How can i extract values of frequency,description and amount and update them to my tableview. Below is my Json Data format that i get from the server request. I am new to swift and the concept of dictionaries and Arrays is quiet confusing to me

{
"payment" =     (
            {
        amount = 100;
        currency = USD;
        description = "Awesome Videos";
        frequency = Day;
    },
            {
        amount = 500;
        currency = USD;
        description = "Awesome Videos";
        frequency = Week;
    },
            {
        amount = 3000;
        currency = USD;
        description = "Awesome Videos";
        frequency = Months;
    }
);

}

我想将它们存储在本地的字典或数组中,然后将其更新到我的表视图中.

I want to store them locally in a dictionary or an array, and update them to my tableview.

这也是我从服务器获取数据的代码

Here also is my code to fetch the data from server

let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
        if (data != nil){
            print(data)
            do {

                // let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers)
                let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)

                print(jsonResult)
                print(jsonResult["payment_plans"])



                if let plan_list = jsonResult as? NSDictionary{
                    print("got dictionary")

                    for (key, value) in plan_list
                    {
                        print(value)
                        print("printing keys")
                        print(key)

                        if let plans = value as? NSDictionary
                        {
                            print("printing values")
                            print(plans)
                            print(plans["currency"])
                        }
                    }
                }

            }   catch{
                print("Json Serialization failed")
            }
        }

    }
    /*
    if (response != nil){
    print(response)
    }
    else{
    print(error)
    }
    }
    */
    task.resume()
}

我被困在这里,如何提取我在字典中得到的值.预先感谢

I am stuck here how to extract the values that i get in the dictionary. Thanks in advance

推荐答案

字典是一个key-value存储,其中您的值具有AnyObject类型.

Dictionary it's a key-value storage where value in your case have AnyObject type.

要将数据转换为DictionaryArray,可以使用

To convert data into Dictionary or Array you can use

let jsonResult = try JSONSerialization.jsonObject(with: data, options: .mutableContainers)

然后创建付款数组

if let payments = jsonResult as? Array{ 

     *iterate here all payments 

}

如果要使用for (key, value) in plan_list,则需要将其更改为for (key, value) in plan_list.enumerate()

If you want to use for (key, value) in plan_list you need to change it's to for (key, value) in plan_list.enumerate()

这篇关于在Swift iOS9中从Json Data提取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆