如何正确解析IOS SWIFT 3中的Google距离矩阵API JSON

如何正确解析IOS SWIFT 3中的Google距离矩阵API JSON,ios,json,swift3,google-distancematrix-api,Ios,Json,Swift3,Google Distancematrix Api,为了在swift中正确解析多维JSON数组,我搜索了很多。从搜索结果中,我能理解的是,它与Android中的retro-fit解析类似。i、 e为每个json响应创建一个解析类。如果我弄错了,请原谅。我是IOS swif的新手 这是我的距离调用的距离矩阵api json输出 { "destination_addresses" : [ "My Destination" ], "origin_addresses" : [ "My Source" ], "rows" : [

为了在swift中正确解析多维JSON数组,我搜索了很多。从搜索结果中,我能理解的是,它与Android中的retro-fit解析类似。i、 e为每个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调用(工作正常)

此代码运行良好。但我知道这不是解决问题的办法

所以请帮帮我


我想以后这段代码中可能会出现一些错误。不确定

最简单、最有效的方法是使用对象映射。像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,
        ])
    }

}

在本教程中了解有关对象映射的更多信息:

最简单、最有效的方法是使用对象映射。像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,
        ])
    }

}

在本教程中了解有关对象映射的更多信息:

我的问题是,我们可以在没有任何其他库的帮助下解析它吗?我的意思是像android?当然可以,但它不如使用对象映射有效。您将得到许多if/guard语句和for循环,这将导致错误(就像您的代码一样)。:-)从Swift 4可以在没有其他库帮助的情况下解析json。我的问题是,我们能在没有其他库帮助的情况下解析它吗?我的意思是像android?当然可以,但它不如使用对象映射有效。您将得到许多if/guard语句和for循环,这将导致错误(就像您的代码一样)。:-)从Swift 4可以在没有其他库帮助的情况下解析json。有可解码的
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,
        ])
    }

}
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.