从解码 json 响应 iOS 中仅获得一项

getting only one item from decoding json response iOS

我收到了 MapBox 的回复 API。我想将 features 数组的每个特征对象存储在一个数组中,然后想从该数组显示到 table 视图。

{
type: "FeatureCollection",
query: [
    "u"
],
features: [{
        id: "country.19678805456372290",
        type: "Feature",
        place_type: [
            "country"
        ],
        relevance: 1,
        properties: {
            wikidata: "Q30",
            short_code: "us"
        },
        text: "United States",
        place_name: "United States",
        matching_text: "US",
        matching_place_name: "US",
        bbox: [
            -179.9,
            18.8163608007951,
            -66.8847646185949,
            71.4202919997506
        ],
        center: [
            -97.9222112121185,
            39.3812661305678
        ],
        geometry: {
            type: "Point",
            coordinates: [
                -97.9222112121185,
                39.3812661305678
            ]
        }
    },
    {
        id: "country.12405201072814600",
        type: "Feature",
        place_type: [
            "country"
        ],
        relevance: 1,
        properties: {
            wikidata: "Q145",
            short_code: "gb"
        },
        text: "United Kingdom",
        place_name: "United Kingdom",
        bbox: [
            -8.74974065661991,
            49.802416901086,
            1.86276379960989,
            60.9093517989553
        ],
        center: [
            -2.36966957036279,
            54.2379333607472
        ],
        geometry: {
            type: "Point",
            coordinates: [
                -2.36966957036279,
                54.2379333607472
            ]
        }
    },
    {
        id: "country.11702916565595680",
        type: "Feature",
        place_type: [
            "country"
        ],
        relevance: 1,
        properties: {
            wikidata: "Q878",
            short_code: "ae"
        },
        text: "United Arab Emirates",
        place_name: "United Arab Emirates",
        bbox: [
            51.4160146192147,
            22.6282410017159,
            56.4814183948386,
            26.094609499407
        ],
        center: [
            54.2561723713588,
            23.8520599823879
        ],
        geometry: {
            type: "Point",
            coordinates: [
                54.2561723713588,
                23.8520599823879
            ]
        }
    },
    {
        id: "country.11871993712476590",
        type: "Feature",
        place_type: [
            "country"
        ],
        relevance: 1,
        properties: {
            wikidata: "Q212",
            short_code: "ua"
        },
        text: "Ukraine",
        place_name: "Ukraine",
        bbox: [
            22.1375690033684,
            44.3152913001972,
            40.2255909904191,
            52.3793529890401
        ],
        center: [
            31.3202829593814,
            49.3227937844972
        ],
        geometry: {
            type: "Point",
            coordinates: [
                31.3202829593814,
                49.3227937844972
            ]
        }
    },
    {
        id: "country.9140805109496660",
        type: "Feature",
        place_type: [
            "country"
        ],
        relevance: 1,
        properties: {
            wikidata: "Q77",
            short_code: "uy"
        },
        text: "Uruguay",
        place_name: "Uruguay",
        matching_text: "URY",
        matching_place_name: "URY",
        bbox: [
            -58.4891219951353,
            -35.0552819973662,
            -53.1014000735687,
            -30.0855740544354
        ],
        center: [
            -56.012396238658,
            -32.7996455126793
        ],
        geometry: {
            type: "Point",
            coordinates: [
                -56.012396238658,
                -32.7996455126793
            ]
        }
    }
],
attribution: "NOTICE: © 2021 Mapbox and its suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service (https://www.mapbox.com/about/maps/). This response and the information it contains may not be retained. POI(s) provided by Foursquare."

}

所以,我创建了一个这样的模型 -

 struct Response: Codable {
      var features: [Feature]
 }

 struct Feature: Codable {
      var id: String!
      var type: String?
      var matching_place_name: String?
      var place_name: String?
      var geometry: Geometry
      var center: [Double]
      var properties: Properties
 }

 struct Geometry: Codable {
      var type: String?
      var coordinates: [Double]
 }

 struct Properties: Codable {
      var address: String?
 }

并编写此代码 -

var suggestedPlacenames: NSMutableArray = []
func doShowSuggestion(usersQuery: String) {
    let urlString = "\(mapbox_api)\(usersQuery).json?access_token=\(mapbox_access_token)"
    let url = URL(string: urlString)
    let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
         guard let data = data, error == nil else {
            print("Error in URLSeesion")
            return
        }
        if let result = try? JSONDecoder().decode(Response.self, from: data) {
            self.suggestedPlacenames.add(result)
            print(self.suggestedPlacenames.count)
        } else {}
        

但是无论 features 中的响应长度如何,我总是从 self.suggestedPlacenames 得到 1 个长度。如何在 self.suggestedPlacenames 中分别获取每个功能对象?

您需要申报

var allFeatures = [Feature]()
do { 
   let result = try JSONDecoder().decode(Response.self, from: data)           
   self.allFeatures = result.features
   print(self.allFeatures.count) 
 }
catch {
   print(error)
}

在您的代码中:

self.suggestedPlacenames.add(result)

您只向数组中添加了一个元素。 您必须附加数组。

// declare as an array of features
var suggestedPlacenames = [Feature]()

self.suggestedPlacenames.append(contentsOf: result.features)

self.suggestedPlacenames += result.features