我已经搜索了很多,以正确解析swift中的多维JSON数组。从搜索结果来看,我能掌握的是它与Android中的复古解析类似。即为每个json响应创建一个解析类。如果我犯了错误,请原谅。我是IOS swif的新手。
这是我的距离呼叫的距离矩阵api json输出
{
"destination_addresses" : [ "My Destination" ],
"origin_addresses" : [
"My Source"
],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "23.3 km",
"value" : 23333 //lastly i take this
},
"duration" : {
"text" : "49 mins",
"value" : 2938
},
"status" : "OK" //then i check this
}
]
}
],
"status" : "OK" //first i check this
}
我是怎么做到的
这是我的api电话(工作正常)
func extract_Validate(jsonData:NSData)
{
var distInMeters = 0
var chkVarOne = "notOK"
let json1 = NSString(data: jsonData as Data, encoding: String.Encoding.utf8.rawValue)
let data = json1!.data(using: String.Encoding.utf8.rawValue, allowLossyConversion: false)
print(data!)
do{
print("Inside do")
let json = try JSONSerialization.jsonObject(with: jsonData as Data, options:.allowFragments) as! NSObject
if let dictionary = json as? [String: Any] {
print("inside dictionary ")
if let detailsDict = dictionary as? NSDictionary {
print("Parse Data")
for (key, value) in detailsDict {
print("Property: \"\(key as! String)\"")
if key as! String == "status" && value as! String == "OK"{
chkVarOne = "OK"
}
if chkVarOne == "OK"
{
if key as! String == "rows"
{
if let elementsDict = value as? NSDictionary {
for(keyEle, valueEle) in elementsDict{
}
}
if let elementsDict = value as? NSArray
{
if let array = elementsDict as? [Any] {
if let firstObject = array.first {
if let distanceSet = firstObject as? NSDictionary{
for(keyDis, valueDis) in distanceSet{
// print("Property: \"\(keyDis as! String)\"")
if keyDis as! String == "elements"
{
if let DistDict = valueDis as? NSDictionary {
for(keyDict, valueDict) in DistDict{
print("Property: \"\(keyDict as! String)\"")
}
}
if let DistDict = valueDis as? NSArray {
// print(DistDict)
if let distArr = DistDict as?[Any]{
if let frst = distArr.first{
//print(frst)
if let distOne = frst as? NSDictionary{
var checkvar = "notOK"
for(ketOneDis, valOneDis) in distOne{
// print("Property: \"\(ketOneDis as! String)\"", valOneDis)
if ketOneDis as! String == "status" && valOneDis as! String == "OK"{
checkvar = "OK"
}
if checkvar == "OK"
{
if ketOneDis as! String == "distance"
{
// print(valOneDis)
if let valtwoDis = valOneDis as? NSDictionary{
for(kDis, vDis) in valtwoDis{
// print("Property: \"\(kDis as! String)\"", vDis)
if kDis as! String == "value"{
distInMeters = vDis as! Int
}
}
if let valTwoDis = valOneDis as? NSArray{
print(valTwoDis)
}
}
}
}
}
}
}
}
}
}
//print(distanceSet,"rttgtgtgtgtgtgtg")
}
if let distSet = firstObject as? NSArray{
print(distSet,"dfdffddfdfdfd")
}
}
}
}
}
//let rows
}
}
} //ending here
}
}
}
catch{
print("error in JSONSerialization")
}
print(distInMeters," is the resulting value")
}
此代码工作正常。但我知道这不是这样做的方法。
所以请帮帮我
我认为以后可能会在此代码中出现一些错误。不确定
答案 0 :(得分:2)
最简单有效的方法是使用对象映射。像Gloss(https://github.com/hkellaway/Gloss)之类的东西可以解决问题。在您的情况下,您将拥有以下类(对象):
<强>响应强>
import Foundation
import Gloss
//MARK: - Response
public struct Response: Glossy {
public let destinationAddresses : [String]!
public let originAddresses : [String]!
public let rows : [Row]!
public let status : String!
//MARK: Decodable
public init?(json: JSON){
destinationAddresses = "destination_addresses" <~~ json
originAddresses = "origin_addresses" <~~ json
rows = "rows" <~~ json
status = "status" <~~ json
}
//MARK: Encodable
public func toJSON() -> JSON? {
return jsonify([
"destination_addresses" ~~> destinationAddresses,
"origin_addresses" ~~> originAddresses,
"rows" ~~> rows,
"status" ~~> status,
])
}
}
<强>元素:强>
import Foundation
import Gloss
//MARK: - Element
public struct Element: Glossy {
public let distance : Distance!
public let duration : Distance!
public let status : String!
//MARK: Decodable
public init?(json: JSON){
distance = "distance" <~~ json
duration = "duration" <~~ json
status = "status" <~~ json
}
//MARK: Encodable
public func toJSON() -> JSON? {
return jsonify([
"distance" ~~> distance,
"duration" ~~> duration,
"status" ~~> status,
])
}
}
<强>行:强>
import Foundation
import Gloss
//MARK: - Row
public struct Row: Glossy {
public let elements : [Element]!
//MARK: Decodable
public init?(json: JSON){
elements = "elements" <~~ json
}
//MARK: Encodable
public func toJSON() -> JSON? {
return jsonify([
"elements" ~~> elements,
])
}
}
<强>距离:强>
import Foundation
import Gloss
//MARK: - Distance
public struct Distance: Glossy {
public let text : String!
public let value : Int!
//MARK: Decodable
public init?(json: JSON){
text = "text" <~~ json
value = "value" <~~ json
}
//MARK: Encodable
public func toJSON() -> JSON? {
return jsonify([
"text" ~~> text,
"value" ~~> value,
])
}
}
创建类后,通过执行以下操作将JSON映射到Object:
let jsonResponse = ..//Your JSON response
guard let distanceResponse = Response(json: jsonResponse) else {
// handle decoding failure here
}
//You can use the distanceResponse object here as an object and access it's values.
在本教程中了解有关对象映射的更多信息:https://www.raywenderlich.com/150322/swift-json-tutorial-2