使用 Alamofire 在 Swift 中解析 JSON
Parsing JSON in Swift with Alamofire
我无法弄清楚如何使用 Swift 4 return 只 JSON 数据的一部分。
这是我需要解析的JSON:
{
"code": 0,
"responseTS": 1571969400172,
"message": "TagPosition",
"version": "2.1",
"command": "http://123.456.7.89:8080/tag=a4da22e02925",
"tags": [
{
"smoothedPosition": [
-0.58,
-3.57,
0.2
],
"color": "#FF0000",
"positionAccuracy": 0.07,
"smoothedPositionAccuracy": 0.07,
"zones": [],
"coordinateSystemId": "687eba45-7af4-4b7d-96ed-df709ec1ced1",
"areaId": "987537ae-42f3-4bb5-8d0c-79fba8752ef4",
"coordinateSystemName": "CoordSys001",
"covarianceMatrix": [
0.04,
0.01,
0.01,
0.05
],
"areaName": "area",
"name": null,
"positionTS": 1571969399065,
"id": "a4da22e02925",
"position": [
-0.58,
-3.57,
0.2
]
}
],
"status": "Ok"
}
到目前为止,我能够 return 所有 "tags" 数组,如下所示。但是,我只需要return "smoothedPosition" 数据。
func newTest() {
Alamofire.request(url).responseJSON { (response) in
if let newjson = response.result.value as! [String: Any]? {
print(newjson["tags"] as! NSArray)
}
}
}
alamofire 是获得我想要的结果的好方法吗?我以前尝试过 Codable 方法,但因为我的 JSON 有很多不同的部分,我发现只得到我需要的部分很混乱。如果有人能给我一些解决此问题的最佳方法的建议,我将不胜感激。
Better not to use NS classes with Swift wherever possible. So instead of NSArray use Array.
要获得 smoothedPosition
你需要解析更多。 tags
给你一个 array of dictionaries
,因此你需要循环数组并获取 tags array
中的每个字典。然后最后你可以得到你的 smoothedPosition
数组。
func newTest() {
Alamofire.request(url).responseJSON { (response) in
if let newjson = response.result.value as? [String: Any], if let tagArray = newjson["tags"] as? [[String: Any]] {
for object in tagArray {
if let smoothedPosition = object["smoothedPosition"] as? [Double] {
print(smoothedPosition)
}
}
}
}
}
此外,您还应该阅读更多关于 codables
到 parse nested data
的信息,并了解 optional binding and chaining
以防止当您 force (!)
可能为 nil 的内容时发生崩溃在某个时候。
您可以使用此站点根据您的回复检查可能的 Codable
结构:https://app.quicktype.io/
为 JSON 响应创建自定义 Codable
结构或 class 是一个不错的选择。您只能实现为您想要访问的成员变量。
struct CustomResponse: Codable {
let tags: [Tag]
}
struct Tag: Codable {
let smoothedPosition: Position
}
struct Position: Codable {
let x: Float
let y: Float
let z: Float
}
然后
Alamofire.request(url).responseJSON { (response as? CustomResponse) in
guard let response = response else { return }
print(response.tags.smoothedPosition)
}
您也可以手动更深入地解析您的 JSON 响应,如其他答案中所述。
我无法弄清楚如何使用 Swift 4 return 只 JSON 数据的一部分。
这是我需要解析的JSON:
{
"code": 0,
"responseTS": 1571969400172,
"message": "TagPosition",
"version": "2.1",
"command": "http://123.456.7.89:8080/tag=a4da22e02925",
"tags": [
{
"smoothedPosition": [
-0.58,
-3.57,
0.2
],
"color": "#FF0000",
"positionAccuracy": 0.07,
"smoothedPositionAccuracy": 0.07,
"zones": [],
"coordinateSystemId": "687eba45-7af4-4b7d-96ed-df709ec1ced1",
"areaId": "987537ae-42f3-4bb5-8d0c-79fba8752ef4",
"coordinateSystemName": "CoordSys001",
"covarianceMatrix": [
0.04,
0.01,
0.01,
0.05
],
"areaName": "area",
"name": null,
"positionTS": 1571969399065,
"id": "a4da22e02925",
"position": [
-0.58,
-3.57,
0.2
]
}
],
"status": "Ok"
}
到目前为止,我能够 return 所有 "tags" 数组,如下所示。但是,我只需要return "smoothedPosition" 数据。
func newTest() {
Alamofire.request(url).responseJSON { (response) in
if let newjson = response.result.value as! [String: Any]? {
print(newjson["tags"] as! NSArray)
}
}
}
alamofire 是获得我想要的结果的好方法吗?我以前尝试过 Codable 方法,但因为我的 JSON 有很多不同的部分,我发现只得到我需要的部分很混乱。如果有人能给我一些解决此问题的最佳方法的建议,我将不胜感激。
Better not to use NS classes with Swift wherever possible. So instead of NSArray use Array.
要获得 smoothedPosition
你需要解析更多。 tags
给你一个 array of dictionaries
,因此你需要循环数组并获取 tags array
中的每个字典。然后最后你可以得到你的 smoothedPosition
数组。
func newTest() {
Alamofire.request(url).responseJSON { (response) in
if let newjson = response.result.value as? [String: Any], if let tagArray = newjson["tags"] as? [[String: Any]] {
for object in tagArray {
if let smoothedPosition = object["smoothedPosition"] as? [Double] {
print(smoothedPosition)
}
}
}
}
}
此外,您还应该阅读更多关于
codables
到parse nested data
的信息,并了解optional binding and chaining
以防止当您force (!)
可能为 nil 的内容时发生崩溃在某个时候。您可以使用此站点根据您的回复检查可能的
Codable
结构:https://app.quicktype.io/
为 JSON 响应创建自定义 Codable
结构或 class 是一个不错的选择。您只能实现为您想要访问的成员变量。
struct CustomResponse: Codable {
let tags: [Tag]
}
struct Tag: Codable {
let smoothedPosition: Position
}
struct Position: Codable {
let x: Float
let y: Float
let z: Float
}
然后
Alamofire.request(url).responseJSON { (response as? CustomResponse) in
guard let response = response else { return }
print(response.tags.smoothedPosition)
}
您也可以手动更深入地解析您的 JSON 响应,如其他答案中所述。