简体   繁体   English

使用SwiftyJson解析Json

[英]Parse Json using SwiftyJson

I am able to get the entire JSon array parsed. 我能够解析整个JSon数组。 It outputs to console with no issue. 它输出到控制台没有问题。 I can't seem to get the individual params from the array... my json looks like: 我似乎无法从数组中获取各个参数……我的json看起来像:

[{
        City = NYC;
        Device = "<null>";
        DisplayAs = "Steve Hutson";
        FirstName = Steve;
        LastName = Hutson;
        MobilePhone = "000-000-0000";
        Org = "<null>";
        Region = "";
        Role = INSPECTOR;
        SupervisorID = "73990";
        email = "email@email.com";
        fLast = shutson;
        "gz_modtimestamp" = "2015-07-28 14:42:41";
        id = 96;
        isActive = YES;
        lastupdated = "<null>";
        sendemail = 1;
        token = "<null>";
        userpassword = "xxx";
    },{
        City = DET;
        Device = "<null>";
        DisplayAs = "Filipe Washington";
        FirstName = Filipe;
        LastName = Washington;
        MobilePhone = "000-000-0000";
        Org = "<null>";
        Region = "";
        Role = INSPECTOR;
        SupervisorID = "6567";
        email = "email@email.com";
        fLast = shutson;
        "gz_modtimestamp" = "2015-07-28 13:02:09";
        id = 93;
        isActive = YES;
        lastupdated = "<null>";
        sendemail = 1;
        token = "<null>";
        userpassword = "xxx";
    }]

In my main ViewController.swift file my json request looks like: 在我的主要ViewController.swift文件中,我的json请求如下所示:

    var myData:NSData = getJSON("http://xxxx/getusersData.php")

    var myDict:NSArray = parseJSON(myData)

    println(myDict)

This prints my entire json object which is perfect. 这将打印我的整个json对象,这是完美的。 However my issue is, how do I get only the FirstName's in an array? 但是我的问题是,如何只在数组中获取名字? By index this works: 通过索引,这有效:

println(myDict[0]["FirstName"]) how ever, it only brings back one item. println(myDict[0]["FirstName"])然而,它只带回一项。

I am trying to insert specific json items into sqlite which i am using SQLite.swift but i need to know how to retrieve specific item parameters as I would if i was using AJAX. 我正在尝试将特定的json项插入使用SQLite.swift的sqlite中,但我需要知道如何像使用AJAX那样检索特定项的参数。

I am trying to retrieve FirstName, Email and UserPassword info. 我正在尝试检索名字,电子邮件和用户密码信息。

Any help would be appreciated. 任何帮助,将不胜感激。

You can iterate over the JSON object and retrieve that info: 您可以遍历JSON对象并检索该信息:

for (index: String, subJson: JSON) in json {
    //Do something you want
    var firstName = subJson["FirstName"].stringValue
    var email = subJson["email"].stringValue
    var userPassword = subJson["userpassword"].stringValue
    // Build the sqlite query with the variables here 
} 

As a clarification, you don't access the values directly as in json["FirstName"] , you must also use the type functions from SwiftyJSON. 为了澄清起见,您不像json["FirstName"]那样直接访问值,还必须使用SwiftyJSON中的类型函数。 In your case they're both strings, so stringValue is used. 在您的情况下,它们都是字符串,因此使用stringValue If you needed and int , it'd be intValue . 如果需要intintValue

There's also the option of just json["FirstName"].string in case the field is optional. 如果该字段是可选的,则还可以选择仅使用json["FirstName"].string Take a look at the readme 看看自述文件

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

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