Swift JSON 提取
Swift JSON extraction
我正在开发 swift iOS 应用程序并从 Web 服务获得以下 JSON 响应。我正在尝试解析并从中获取 nextResponse
。我无法提取它。有人可以指导我解决这个问题吗?
listofstudents:
({
studentsList = (
{
data = (
"32872.23",
"38814.87",
"38915.85"
);
label = “name, parents and guardians”;
}
);
dateList = (
"Apr 26, 2017",
"Jun 10, 2017",
"Jul 26, 2017"
);
firstResponse = “This school has 1432 students and 387 teachers.”;
nextResponse = “This school has around 1400 students.”;
})
Swift代码:
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
print("json: \(json)")
if let parseJSON = json {
let finalResponse = parseJSON["listofstudents"] as? AnyObject
print("listofstudents:: \(finalResponse)")
let nextResponse = parseJSON["nextResponse"] as? AnyObject
print("nextResponse:: \(nextResponse)")
}
} catch {
print(error)
}
nextResponse
是 JSON 结构的一部分(它是一个嵌套节点)。所以你应该使用以下方式访问它:
typealias JSON = [String: Any]
if let finalResponse = parseJSON["listofstudents"] as? JSON {
let nextResponse = finalResponse ["nextResponse"] as? JSON
print("nextResponse:: \(nextResponse)")
}
看起来你的 listofstudents 是一个字典数组,所以尝试迭代它并提取它:-
if let finalResponse = parseJSON["listofstudents"] as? [String: Any] {
//If your finalResponse has list then you can print all the data
for response in finalResponse {
let nextResponse = finalResponse ["nextResponse"] as? AnyObject
print("nextResponse::\(nextResponse)")
}
}
不要在 Swift 中使用 NSDictionary
,而是使用其原生的 Swift 对应物 Dictionary
。这是您访问嵌入在其他词典中的词典的方式:
do {
guard let json = try JSONSerialization.jsonObject(with: data!) as? [String:Any] else {return}
print("json: \(json)")
guard let finalResponse = parseJSON["listofstudents"] as? [String:Any] else {return}
print("listofstudents:: \(finalResponse)")
guard let nextResponse = finalResponse["nextResponse"] as? [String:Any] else {return}
print("nextResponse:: \(nextResponse)")
} catch {
print(error)
}
我正在开发 swift iOS 应用程序并从 Web 服务获得以下 JSON 响应。我正在尝试解析并从中获取 nextResponse
。我无法提取它。有人可以指导我解决这个问题吗?
listofstudents:
({
studentsList = (
{
data = (
"32872.23",
"38814.87",
"38915.85"
);
label = “name, parents and guardians”;
}
);
dateList = (
"Apr 26, 2017",
"Jun 10, 2017",
"Jul 26, 2017"
);
firstResponse = “This school has 1432 students and 387 teachers.”;
nextResponse = “This school has around 1400 students.”;
})
Swift代码:
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
print("json: \(json)")
if let parseJSON = json {
let finalResponse = parseJSON["listofstudents"] as? AnyObject
print("listofstudents:: \(finalResponse)")
let nextResponse = parseJSON["nextResponse"] as? AnyObject
print("nextResponse:: \(nextResponse)")
}
} catch {
print(error)
}
nextResponse
是 JSON 结构的一部分(它是一个嵌套节点)。所以你应该使用以下方式访问它:
typealias JSON = [String: Any]
if let finalResponse = parseJSON["listofstudents"] as? JSON {
let nextResponse = finalResponse ["nextResponse"] as? JSON
print("nextResponse:: \(nextResponse)")
}
看起来你的 listofstudents 是一个字典数组,所以尝试迭代它并提取它:-
if let finalResponse = parseJSON["listofstudents"] as? [String: Any] {
//If your finalResponse has list then you can print all the data
for response in finalResponse {
let nextResponse = finalResponse ["nextResponse"] as? AnyObject
print("nextResponse::\(nextResponse)")
}
}
不要在 Swift 中使用 NSDictionary
,而是使用其原生的 Swift 对应物 Dictionary
。这是您访问嵌入在其他词典中的词典的方式:
do {
guard let json = try JSONSerialization.jsonObject(with: data!) as? [String:Any] else {return}
print("json: \(json)")
guard let finalResponse = parseJSON["listofstudents"] as? [String:Any] else {return}
print("listofstudents:: \(finalResponse)")
guard let nextResponse = finalResponse["nextResponse"] as? [String:Any] else {return}
print("nextResponse:: \(nextResponse)")
} catch {
print(error)
}