我能够对整个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";
}]
在我的主ViewController.swift文件中,json请求如下所示:
var myData:NSData = getJSON("http://xxxx/getusersData.php")
var myDict:NSArray = parseJSON(myData)
println(myDict)
这个打印了我的整个json对象,非常完美。然而,我的问题是,如何只获得数组中的FirstName?根据索引,这是可行的:
println(myDict[0]["FirstName"])
无论如何,它只带回一个项目。
我正在尝试将特定的json项插入到我使用SQLite.swift的sqlite中,但我需要知道如何像使用AJAX那样检索特定的项参数。
我试图检索FirstName,电子邮件和UserPassword信息。
任何帮助都将不胜感激。
发布于 2015-08-17 22:31:10
您可以遍历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
}
澄清一下,您不像在json["FirstName"]
中那样直接访问这些值,您还必须使用来自SwiftyJSON的类型函数。在您的示例中,它们都是字符串,因此使用了stringValue
。如果你需要的话,还有int
,那就是intValue
。
如果字段是可选的,也可以选择只使用json["FirstName"].string
。看一看自述文件
https://stackoverflow.com/questions/32059937
复制相似问题