Swift将JSON值解析为全局数组

时间:2017-05-16 03:47:42

标签: ios arrays json swift parsing

我一直试图让下面的代码返回一个可以在UIPickerView类“行标题中使用的信息数组。代码不起作用,我现在这样做,当我运行打印脚本时,它确实返回一个我的JSON值中的所有值的列表。解析JSON的示例。我现在一直在观看YouTube视频并阅读有关此事的信息。任何帮助都会很棒。我找到的所有内容都将Parsed JSON结果发送到表格视图。

       {
    date = "2017-05-01";
    "financial_institution" = "Your Neighbourhood C.U.";
    "five_year" = "2.79";
    "four_year" = "3.15";
    key = 86;
    "one_year" = "2.79";
    "six_months" = 0;
    "three_year" = "3.09";
    "two_year" = "2.89";
    "variable_rate" = 0;
}

        func getJSONData()
    {
        let url = URL(string:"")
        let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in

    if error != nil {
        print("error")
    }

    else {

        if let mydata = data {

            do {

                let myJson = try JSONSerialization.jsonObject(with: mydata, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject

               print(myJson)

                var lenderName = myJson["financial_institution"]

                for i in lenderName {

                    var lender = "financial_institution"
                    lender += "\(i)"
                    var lenderList = myJson[lender].string as String!

                    lenderNamesArray.append(lenderList)
                }


            }

                catch {
                    // catch error
                }
            }
        }
    }

task.resume()

    }

1 个答案:

答案 0 :(得分:-1)

我假设您正在分析的JSON包含许多贷方,例如:

    {
    "lenders": [
        {
        "date": "2017-05-01",
        "financial_institution": "Your Neighbourhood C.U.",
        "five_year": "2.79",
        "four_year": "3.15",
        "key": "86",
        "one_year": "2.79",
        "six_months": "0",
        "three_year": "3.09",
        "two_year": "2.89",
        "variable_rate": "0"
        },
        {
        "date": "2017-05-01",
        "financial_institution": "Your Neighbourhood C.U.",
        "five_year": "2.79",
        "four_year": "3.15",
        "key": "86",
        "one_year": "2.79",
        "six_months": "0",
        "three_year": "3.09",
        "two_year": "2.89",
        "variable_rate": "0"
        },
        {
        "date": "2017-05-01",
        "financial_institution": "Your Neighbourhood C.U.",
        "five_year": "2.79",
        "four_year": "3.15",
        "key": "86",
        "one_year": "2.79",
        "six_months": "0",
        "three_year": "3.09",
        "two_year": "2.89",
        "variable_rate": "0"
        }]
    }

由于我无法使用您的数据对此进行测试,因此您的代码看起来像这样:

func getJSONData()
{
    let url = URL(string: "");

    let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in

        if error != nil
        {
            print("error")
        }

        else
        {
            if let mydata = data
            {
                do
                {
                    let myJson = try JSONSerialization.jsonObject(with: mydata, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject

                    let lenders = myJson["lenders"]  as! [AnyObject];

                    for lender in lenders
                    {
                        // This is how you access the data
                        let institution = lender["financial_institution"] as! String;
                        let fiveYear = lender["five_year"] as! CGFloat;

                        // Store the data however you are doing it
                    }
                }

                catch {
                    // catch error
                }
            }
        }
    }

    task.resume()

}